Good morning every body;
I’m trying to make a database class in android, and it is done, I’ve created a table successfully too. The problem comes when I want to insert rows to the table using ContentValues, it tries to insert the row in different order from the table fields which cuases an error in the logcat saying : no such table as follows:
These are my variables:
private static final String row_ID="RID";
private static final String row_NAME="NAME";
private static final String row_EMAIL="EMAIL";
private static final String row_WEBSITE="WEBSITE";
private static final String row_TELEPHONE1="PHONE_NUMBER1";
private static final String row_TELEPHONE2="PHONE_NUMBER2";
private static final String row_TELEPHONE3="PHONE_NUMBER3";
private static final String row_TELEPHONE4="PHONE_NUMBER4";
private static final String row_TELEPHONE5="PHONE_NUMBER5";
private static final String db_NAME="ContactsDb";
private static final String tab_NAME="ContactsTab";
private static final int db_VERSION=2;
private dbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
My table :
String CREATE_TABLE="CREATE TABLE " + db_NAME + " ("
+ row_ID + " INT PRIMARY KEY AUTOINCREMENT,"
+ row_NAME + " TEXT, "
+ row_EMAIL + " TEXT, "
+ row_WEBSITE + " TEXT, "
+ row_TELEPHONE1 + " TEXT, "
+ row_TELEPHONE2 + " TEXT, "
+ row_TELEPHONE3 + " TEXT, "
+ row_TELEPHONE4 + " TEXT, "
+ row_TELEPHONE5 + " TEXT);";
db_name.execSQL(CREATE_TABLE);
Inserting rows using ContentValues :
public long createEntry(String name, String email, String website, String telephone1, String telephone2, String telephone3, String telephone4, String telephone5)
{
ContentValues cv = new ContentValues ();
Log.v("STATUS", "getting input data..");
cv.put(row_NAME, name);
cv.put(row_EMAIL, email);
cv.put(row_WEBSITE, website);
cv.put(row_TELEPHONE1, telephone1);
cv.put(row_TELEPHONE2, telephone2);
cv.put(row_TELEPHONE3, telephone3);
cv.put(row_TELEPHONE4, telephone4);
cv.put(row_TELEPHONE5, telephone5);
Log.v("STATUS", "got input data, inserting data....");
return ourDatabase.insert(tab_NAME, null, cv);
}
So, when I run this application it succeeds doing all these until it arrives to ourDatabase.insert(tab_NAME, null, cv); it shows the error:
SQLiteLog (1) no such table: ContactsTab
SQLiteDatabase Error inserting Name=Ahlam M. Hussain PHONE_NUMBER5=
PHONE_NUMBER4= PHONE_NUMBER3= PHONE_NUMBER2=059999999
PHONE_NUMBER1= 09299999 EMAIL=ahlam@ahlam.com EWEBSITE=
SQLiteDatabase android.database.sqlite.SQLiteException: no such table:
ContactsTab (code 1): ,while compiling: INSERT INTO
ContactsTab(NAME, PHONE_NUMBER5 PHONE_NUMBER4 PHONE_NUMBER3
PHONE_NUMBER2 PHONE_NUMBER1, EMAIL, WEBSITE) VALUES
(?,?,?,?,?,?,?,?)
You can notice that it is trying to insert row in different fields orderm so it is not recognizing the table..
So what to do ??? I really got suck of that..
You forget to set brace on
it has to be
EDIT
try it with “CREATE TABLE IF NOT EXISTS” and “INTEGER PRIMARY KEY AUTOINCREMENT” not “INT PRIMARY KEY AUTOINCREMENT”. Also You had set db_name instead of table_name in your CREATE-TABLE String. Change it to your table_name: