I have a problem on adding subcategories,I want for example as discribed in the table below,to write a method in android that takes the parent id wich is the id of Animal and put it in the column parentid of doggie.
| id | parentid | name |
-----------------------------------------
| 1 | null | animal |
| 2 | null |vegetable |
| 3 | 1 | doggie |
| 4 | 2 | carrot |
| | | |
| | | |
this is my query to create the table in android:
private static final String CREATE_TABLE_CATEGORIES="CREATE TABLE "+TABLE_CATEGORIES+"(id INTEGER PRIMARY KEY AUTOINCREMENT,"+category_name+
" TEXT,parentid INTEGER null,foreign key (parentid) references "+TABLE_CATEGORIES+" (id));";
then :
public void onCreate(SQLiteDatabase db){
//Creation required tables
db.execSQL(CREATE_TABLE_CATEGORIES);
}
........... ........... ...........
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
// on upgrade drop older tables
db.execSQL("PRAGMA foreign_keys = ON;");
db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_CATEGORIES);
// create new table
onCreate(db);
}
Now I want to link a child categorie with the parent categorie using update.
public void AddSubCategory(Categories parent,Categories child){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(String.valueOf(child.getParentid()),parent.getId());
db.update(TABLE_CATEGORIES,values,KEY_ID + " = ?",new String[] {String.valueOf(child.getId())});
}
The error:
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "0": syntax error (code 1): , while compiling: UPDATE Categories SET 0=? WHERE id = ?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)