android.database.sqlite。SQLiteConstraintException:columnidisnotunique android.database.sqlite.SQLiteConstraintException(code19)

时间:2021-01-30 05:32:20

first time when inserting the row it works without any issues. But the after installing the app in same device(installing from eclipse without uninstall the app from device) and trying to insert new row it throws exception

第一次插入行时,它没有任何问题。但是,在将应用程序安装到同一设备后(从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)

Create Table:

创建表:

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

Insert:

插入:

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

Upgrade:

升级:

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

    onCreate(db);
}

Exception when inserting:

插入时异常:

    db.insert(TABLE_USER_DATA, null, values);

1 个解决方案

#1


1  

The error is telling you that you're trying to create two rows with the same value for column ID which is defined as PRIMARY KEY. In order to let sqlite create auto-increment ID values for you, just remove this line.-

这个错误告诉您,您正在尝试为列ID创建两个具有相同值的行,列ID被定义为主键。为了让sqlite为您创建自动递增的ID值,只需删除这一行

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

and declare the column as INTEGER.

并将列声明为整数。

#1


1  

The error is telling you that you're trying to create two rows with the same value for column ID which is defined as PRIMARY KEY. In order to let sqlite create auto-increment ID values for you, just remove this line.-

这个错误告诉您,您正在尝试为列ID创建两个具有相同值的行,列ID被定义为主键。为了让sqlite为您创建自动递增的ID值,只需删除这一行

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

and declare the column as INTEGER.

并将列声明为整数。