第一次插入行时,它没有任何问题。但是在同一设备上安装应用程序(从eclipse安装而不从设备卸载应用程序)并尝试插入新行之后,它将引发异常

10-1216: 36: 38.405: E/AndroidRuntime(32429): Causedby:android.database.sqlite.SQLiteConstraintException: columnidisnotunique(code19)
10-12 16:36:38.405: E/AndroidRuntime(32429): Caused by: android.database.sqlite.SQLiteConstraintException: column id is not unique (code 19)
10-12 16:36:38.405: E/AndroidRuntime(32429):    at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
10-12 16:36:38.405: E/AndroidRuntime(32429):    at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:727)
10-12 16:36:38.405: E/AndroidRuntime(32429):    at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
10-12 16:36:38.405: E/AndroidRuntime(32429):    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
10-12 16:36:38.405: E/AndroidRuntime(32429):    at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1565)
10-12 16:36:38.405: E/AndroidRuntime(32429):    at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1514)


创建表:

    public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_USER_DATA + "("
            + KEY_ID + " TEXT PRIMARY KEY," + KEY_TIMESTAMP + " TEXT,"
            + KEY_JSON_DATA + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}


插入:

    public void addJsonData(JSONDataStore dataStore) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_ID, dataStore.getKey());
    values.put(KEY_TIMESTAMP, dataStore.getTimeStamp());
    values.put(KEY_JSON_DATA, dataStore.getJsonData());

    db.insert(TABLE_USER_DATA, null, values);
}


升级:

    @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER_DATA);

    onCreate(db);
}


插入时的异常:

    db.insert(TABLE_USER_DATA, null, values);

最佳答案

该错误告诉您正在尝试为定义为ID的列PRIMARY KEY创建具有相同值的两行。为了让sqlite为您创建自动递增ID值,只需删除此行即可。-

values.put(KEY_ID, dataStore.getKey());


并将该列声明为INTEGER

关于android - android.database.sqlite.SQLiteConstraintException:columnidisnotunique(code19)android.database.sqlite.SQLiteConstraintException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19333533/

10-10 07:08