Reyjohn
Reyjohn

Reputation: 2734

android.database.sqlite.SQLiteConstraintException can not be fixed

I have a insertion method which cheks first if the row is available or not, if not then it tries to create new or if available then it updates that, but this method giving me the exception in the else part, here is my method

public long SetSettings(String whichCol, String value) {
        // TODO Auto-generated method stub
        preferences = ourcontext.getSharedPreferences(filenames, 0);
        String[] colum = new String[] { KEY_CAR };
        Cursor c = ourDB.query(DBhelper.DATABASE_SETTINGSTABLE, colum, KEY_CAR
                + "=" + "'" + preferences.getString("selectedcar", "") + "'",
                null, null, null, null);
        ContentValues cvedit = new ContentValues();
        cvedit.put(KEY_CAR, preferences.getString("selectedcar", ""));
        cvedit.put(whichCol, value);
        if (c.getCount() > 0) {

            return ourDB.update(
                    DBhelper.DATABASE_SETTINGSTABLE,
                    cvedit,
                    KEY_CAR + "=" + "'"
                            + preferences.getString("selectedcar", ""), null);

        } else {

            return ourDB.insert(DBhelper.DATABASE_SETTINGSTABLE, null, cvedit);

        }
    }

and this is how I created my database:

public static final String KEY_ID = "_id";
public static final String KEY_CAR = "car";   
public static final String KEY_SET_DISENTRY_MODE = "ditance_entry_mode";
        public static final String KEY_SET_DISUNIT = "distance_unit";
        public static final String KEY_SET_PETROLUNIT = "petrol_unit";
        public static final String KEY_SET_CONDISPLAY = "consumption_display";
        public static final String KEY_SET_DATEFORMAT = "date_format";
        public static final String KEY_SET_CURRENCY = "currency";
        public static final String KEY_SET_STOREFILLLOC = "store_fillloc";
        public static final String KEY_SET_GPSPIN = "gps_pin";
        public static final String KEY_SET_KEYCLICK = "keyboard_click";
        public static final String KEY_SET_KEYVIBRATE = "keyboard_vibrate";
        public static final String KEY_SET_BACKUP = "backup";

@Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL("CREATE TABLE " + DATABASE_SETTINGSTABLE + " (" + KEY_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_CAR
                + " TEXT NOT NULL, " + KEY_SET_DISENTRY_MODE + " TEXT, "
                + KEY_SET_DISUNIT + " TEXT, " + KEY_SET_PETROLUNIT + " TEXT, "
                + KEY_SET_CONDISPLAY + " TEXT, " + KEY_SET_DATEFORMAT
                + " TEXT, " + KEY_SET_CURRENCY + " TEXT, "
                + KEY_SET_STOREFILLLOC + " TEXT, " + KEY_SET_GPSPIN + " TEXT, "
                + KEY_SET_KEYCLICK + " TEXT, " + KEY_SET_KEYVIBRATE + " TEXT, "
                + KEY_SET_BACKUP + " TEXT NOT NULL);");

    }

I cant figure out where is my problem, any help?

Upvotes: 0

Views: 103

Answers (1)

nandeesh
nandeesh

Reputation: 24820

you have a constraint KEY_SET_BACKUP TEXT NOT NULL but when updating you are passing null as this value, so add below line too to the contentvalues

cvedit.put(KEY_SET_BACKUP,"");

Upvotes: 2

Related Questions