1

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)

2 Answers 2

1

The first argument to ContentValues put() should be the column name such as "parentid" and not a number such as 0 returned by child.getParentid().

Sign up to request clarification or add additional context in comments.

3 Comments

But what about the String.valueof() method,it doesnt return a String?
Is there a method to convert the returned int by getParentid(),into String?
Yes it's a String but the string does not represent a column name.
0

Your update statement should be :

values.put("parentid",parent.getId()); db.update(TABLE_CATEGORIES,values,KEY_ID + " = ?",new String[{String.valueOf(child.getId())});

2 Comments

Yeah thanks that's what I did,but is there a method That gets parentid of the object instead of writing the "parentid"
I'm not sure I get your question completely. "parentid" is the name of the column. If you want to get the parentid of an object, you will need to query your database instead. You can then do something like cursor.getColumnName(int columnIndex)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.