Reputation: 745
I'm very new to droid development but I was working on some SQL. I have an class which extends the SQLOpenDatabaseHelper and in the onCreate() method I execute this :
public static final String CREATE_DB = "CREATE TABLE " + TABLE_VOCAB_INFO
+ "(" + COLUMN_NAME + " TEXT NOT NULL PRIMARY KEY," + COLUMN_URL
+ " TEXT NOT NULL," + COLUMN_DOWNLOADED + "TEXT NOT NULL);";
However, when I try to add something to the database like so :
//Adding method
ContentValues cv = new ContentValues();
cv.put(COLUMN_NAME, "item " + i);
cv.put(COLUMN_URL, "random url");
cv.put(COLUMN_DOWNLOADED, "true");
open();
database.insert(TABLE_VOCAB_INFO, null, cv);
I get the error:
(1) no such table: vocab_info
How would I resolve this?
EDIT: more info.
I am creating the database in the onCreate() method of the helper like this:
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_DB);
} catch (SQLiteException e) {
System.out.println("Error with creation");
}
}
I access the database using the adding method like this
public void onClick(View viewSelected) {
switch (viewSelected.getId()) {
case R.id.bDownload:
DatabaseManipulator dm = new DatabaseManipulator(this);
dm.open();
dm.addList(null);
dm.addList(null);
dm.addList(null);
dm.addList(null);
dm.addList(null);
for (VocabList list : dm.getAllLists()) {
System.out.println(list.getName());
}
dm.close();
break;
Thanks in advance
Upvotes: 1
Views: 267
Reputation: 27549
1) try below create table statement.
public static final String CREATE_DB = "CREATE TABLE " + TABLE_VOCAB_INFO
+ "(" + COLUMN_NAME + " TEXT NOT NULL PRIMARY KEY," + COLUMN_URL
+ " TEXT NOT NULL," + COLUMN_DOWNLOADED + " TEXT NOT NULL);";
2) If you making any changes to table structure
or adding new table, You ll need to uninstall and re-install
your application , as onCreate
use to call once if there is no DB created, If DB is already there onCreate wont be called and your changes will not gonna affect DB.
Worked for OP >> you can also do this by changing DB version.
Upvotes: 1