我在将值插入sqllite数据库时遇到问题。
private static final String SQL_CREATE_TABLE_METER = "create table "
+ TABLE_METER + " ( "
+ METER_KEY_ID + " integer primary key autoincrement, "
+ METER_KEY_METER_ID + " text unique, "
+ METER_KEY_ACCOUNT_ID + " text, "
+ METER_KEY_MODEL + " text, "
+ METER_KEY_TYPE + " text, "
+ METER_KEY_LOCATION + " text, "
+ METER_KEY_ACCRUED + " double, "
+ METER_KEY_CURRENT + " double, "
+ METER_KEY_INSTALLATION_ACT_CODE + " text, "
+ METER_KEY_INSTALLATION_LOCATION_ID + " text );";
所以我的METER_KEY_ID
是唯一的。
我正在添加新的仪表并显示带有新插入ID的日志。
public void addMeters(List<Meter> meters, String accountId) {
mDb.beginTransaction();
try {
for(int i = 0; i < meters.size(); i++) {
Log.d("VodomerDBHelper", meters.get(i).id);
mDb.insert(TABLE_METER, null, createMeterValues(meters.get(i), accountId));
}
mDb.setTransactionSuccessful();
} finally {
mDb.endTransaction();
}
}
我的日志显示下一个:
03-24 13:13:27.126: D/VodomerDBHelper(7299): 29980
03-24 13:13:28.596: D/VodomerDBHelper(7299): 29981
03-24 13:13:29.396: D/VodomerDBHelper(7299): 29983
03-24 13:13:29.426: D/VodomerDBHelper(7299): 29982
03-24 13:13:29.756: D/VodomerDBHelper(7299): 29984
03-24 13:13:30.056: D/VodomerDBHelper(7299): 29985
所以所有的价值观都不同。但我有下一个例外
03-24 13:13:27.166: E/SQLiteDatabase(7299): Error inserting id=29980 installation_location_id=93129966 model=СКВ-15/Х ВIР-М account_id=3072460011 location=Сан.узел current=0.0 installation_act_code=29935 type=расход холодной вод accrued=4.0
03-24 13:13:27.166: E/SQLiteDatabase(7299): android.database.sqlite.SQLiteConstraintException: column id is not unique (code 19)
03-24 13:13:27.166: E/SQLiteDatabase(7299): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
03-24 13:13:27.166: E/SQLiteDatabase(7299): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
接下来我检查我的sqllite数据库,一切正常。新数据现在和现在都存在问题。 但为什么会抛出异常?
答案 0 :(得分:0)