错误
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();
}
有人知道为什么会这样吗? ................................................... ................................................... ....................................
答案 0 :(得分:1)
您可能会使用 id 列的值( KEY_ID )进行插入。该列的赔率定义为 INTEGER PRIMARY KEY 或 INTEGER 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 )的值。
您可能会发现SQLite Autoincrement很有帮助。
答案 1 :(得分:0)
您似乎犯了与我相同的新手错误。使用 db.update 修改行值时,请勿通过将KEY_ID添加到ContentValues
来指定哪一行。相反,您必须为此使用where子句。 where子句是传递给db.update的参数之一。
说明:ContentValues
包含要应用于选定行的列名/值对。 where子句指示要选择的行。可以(并且很常见)选择多行。为 where 子句传递null会更新数据库中的每一行-请当心