gpt4 book ai didi

java - android.database.sqlite.SQLiteConstraintException : UNIQUE constraint failed:

转载 作者:行者123 更新时间:2023-11-29 23:26:15 26 4
gpt4 key购买 nike

错误

 FATAL EXCEPTION: main
Process: com.appmaster.akash.messageplus, PID: 14373
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: UserData.RecieversID (code 1555)
at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734)
at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1579)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1525)
at com.appmaster.akash.messageplus.Chat.mMessagesSent(Chat.java:918)
at com.appmaster.akash.messageplus.Chat.sendMessage(Chat.java:709)
at com.appmaster.akash.messageplus.Chat.access$900(Chat.java:76)
at com.appmaster.akash.messageplus.Chat$5.onClick(Chat.java:442)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

插入

 MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, MessageRecieverId);
contentValues.put(KEY_NAME, MessageRecieverName);
contentValues.put(KEY_MESSAGES_SENT, 0);
contentValues.put(KEY_MESSAGES_RECIEVED, 0);
contentValues.put(KEY_TIME_SPENT, "");

long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
if (returnVariable == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();

插入没有问题,但更新时出现此错误

正在更新

 MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();int userData = 0;
Cursor data2 = helper.getUserData();
while (data2.moveToNext()) {
userData = data2.getInt(data2.getColumnIndex("MessagesSent"));
}
ContentValues contentValues2 = new ContentValues();
contentValues2.put(KEY_ID, MessageRecieverId);
contentValues2.put(KEY_MESSAGES_SENT, userData+1);
long returnVariable2 = db.update(TABLE_USER_DATA, contentValues2,null,null);
if (returnVariable2 == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
//-1 means there was an error updating the values
} else {
Toast.makeText(getApplication(),"uf", Toast.LENGTH_SHORT).show();
}

谁知道为什么会这样? ..................................................... ..................................................... ...........................................................

最佳答案

您可能使用 id 列 (KEY_ID) 的值进行插入。此列上的赔率定义为 INTEGER PRIMARY KEYINTEGER PRIMARY KEY AUTOINCREMENT

通常您不会为此类列指定值,因为 SQLite 会分配一个唯一值(通常比分配的最高值大 1)。

如果您为这样的列指定一个值,并且表中的一行已经在该列中具有该值,则将违反 PRIMARY KEY 隐含的 UNIQUE 约束因为这违反了 PRIMARY KEY 必须是唯一 的规则。

我建议您使用:-

MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
//contentValues.put(KEY_ID, MessageRecieverId); //<<<<<<<<<< COMMENTED OUT (can delete the line)
contentValues.put(KEY_NAME, MessageRecieverName);
contentValues.put(KEY_MESSAGES_SENT, 0);
contentValues.put(KEY_MESSAGES_RECIEVED, 0);
contentValues.put(KEY_TIME_SPENT, "");

long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
if (returnVariable == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();

returnVariable 将成为插入行时分配的 id 列 (KEY_ID) 的值。

  • 注意 id 列并不一定意味着该列被命名为 id,而是由于它的定义方式,它是一个特殊的列。也就是说,通过指定 INTEGER PRIMARY KEY,该列实际上是通常隐藏的 rowid 列的别名(除非 TABLE 是用 WITHOUT ROWID 短语定义的)。

您可能会发现阅读 SQLite Autoincrement有帮助。

关于java - android.database.sqlite.SQLiteConstraintException : UNIQUE constraint failed:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53489427/

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