android 数据库 unique,java - android.database.sqlite.SQLiteConstraintException: UNIQUE constraint faile...

这篇博客讨论了在Android应用中遇到的一个SQLite数据库错误,具体是当尝试更新UserData表时遇到了UNIQUE约束失败的问题。在尝试插入新数据并成功后,更新现有数据时发生了错误,返回值为-1,表明更新操作失败。作者提供了相关的代码片段来展示问题所在,并寻求解决方案。
摘要由CSDN通过智能技术生成

Error

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)

Inserting

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

No problem in inserting but while updating im getting this error

Updating

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

}

Anyone know why this is so? .................................................................................................................................................

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值