我正在制作一个应用程序,我需要在其中保存一些值sqlitedatabase
,为此我编写了这个函数
public long saveMood(Mood mood) {
openDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_ACTIVE, (mood.isActive() ? 1 : 0));
cv.put(COL_BLOCK_CALL, (mood.isBlockCall() ? 1 : 0));
cv.put(COL_BLOCK_CALL, (mood.isBlockCall() ? 1 : 0));
cv.put(COL_BLOCK_SMS, (mood.isBlockSms() ? 1 : 0));
cv.put(COL_SILENT, (mood.isSilent() ? 1 : 0));
cv.put(COL_VIBRATE, (mood.isVibrate() ? 1 : 0));
cv.put(COL_WIFI, (mood.isWifiOn() ? 1 : 0));
cv.put(COL_TIME, mood.getTime());
cv.put(COL_NAME, mood.getName());
cv.put(COL_VOLUME, mood.getRingVolumn());
long id = database.insert(TABLE_NAME, null, cv);
closeDatabase();
return id;
}
我用来创建表的查询是这个
private static final String CREATE_TABLE_QUERY = "CREATE TABLE " + TABLE_NAME
+ "("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
+ COL_NAME + " TEXT NOT NULL, "
+ COL_ACTIVE + " INTEGER NOT NULL, "
+ COL_BLOCK_CALL + " INTEGER NOT NULL, "
+ COL_BLOCK_SMS + " INTEGER NOT NULL, "
+ COL_BRIGHT + " INTEGER NOT NULL, "
+ COL_SILENT + " INTEGER NOT NULL, "
+ COL_TIME + " LONG NOT NULL, "
+ COL_VIBRATE + " INTEGER NOT NULL, "
+ COL_VOLUME + " INTEGER NOT NULL, "
+ COL_WIFI + " INTEGER NOT NULL "
+ " ); ";
但是每次插入 id = -1 时我都会遇到此异常
Error inserting silent=1 call_block=1 time=0 wifi=0 name=night volume=0 sms_block=1 vibrate=0 active=0
08-19 11:09:05.790: E/Database(6648): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1658)
08-19 11:09:05.790: E/Database(6648): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1519)
请帮帮我!