android.database.sqlite.SQLiteConstraintException无法修复

时间:2012-08-12 17:03:35

标签: java android

我有一个插入方法,如果行可用或不可用,则首先查询,如果没有,则尝试创建新的或者如果可用则更新它,但此方法在else部分给出了例外,这是我的方法

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);

        }
    }

这就是我创建数据库的方式:

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);");

    }

我无法弄清楚我的问题在哪里,有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

你有一个约束KEY_SET_BACKUP TEXT NOT NULL但是在更新时你将null作为这个值传递,所以也将下面的行添加到contentvalues

cvedit.put(KEY_SET_BACKUP,"");