gpt4 book ai didi

java - android.database.sqlite.SQLiteConstraintException 无法修复

转载 作者:行者123 更新时间:2023-12-02 07:38:07 25 4
gpt4 key购买 nike

我有一个插入方法,它首先检查该行是否可用,如果不可用,则尝试创建新行,或者如果可用,则更新该行,但此方法在 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);");

}

我不知道我的问题出在哪里,有什么帮助吗?

最佳答案

您有一个约束KEY_SET_BACKUP TEXT NOT NULL,但在更新时您将 null 作为此值传递,因此也将以下行添加到内容值中

cvedit.put(KEY_SET_BACKUP,"");

关于java - android.database.sqlite.SQLiteConstraintException 无法修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11924066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com