收录日期:2020/10/31 21:14:19 时间:2010-09-07 01:11:48 标签:android

i created a database with 6 columns and i have a create and update method in my class that takes 6 parameters/arguments which represent these columns. my problem is that, anytime i try to update or create the database without using all 6 arguments (setting some to null), i get an error "constraint failed". this is most particular with the update method.

any ideas how i can get around this? because sometimes i don't want to fill all columns. I have removed the "text not null" constraint when creating the database. Thank you.

You're going to want to use the ContentValues to achieve this. Heres a quick demo.

My function

public boolean updateStuff(int id,ContentValues args) {
        return mDb.update(TableName, args, _id_col + "=" + id, null) > 0;
    }

And to call it. Note you can put as many ContentValues as you need

ContentValues initValues = new ContentValues();
initValues.put(col_key,col_value);

Edit: mDB is a SQLiteDatabase