MainData helper = new MainData(this);name
SQLiteDatabase db = helper.getWritableDatabase();
String uid = MessageRecieverId;
int userData = 0;
Cursor data2 = helper.getUserData();
while (data2.moveToNext()) {
userData = data2.getInt(data2.getColumnIndex(KEY_MESSAGES_SENT));
}
ContentValues contentValues2 = new ContentValues();
contentValues2.put(KEY_ID, uid);
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();
}
}
userData + 1应该只对KEY_ID = uid的那一行执行,但是它会将所有行的值更改为+1,而不是仅执行该操作……MessageRecieverId是不同的,但是将其更改为所有行的数据……任何人都可以帮忙我可以解决这个问题吗?
助手
String userQuery = "CREATE TABLE IF NOT EXISTS " + TABLE_USER_DATA + "(" + KEY_ID + " TEXT, " + KEY_NAME
+ " TEXT, " + KEY_MESSAGES_SENT + " INTEGER, " + KEY_MESSAGES_RECIEVED + " INTEGER, "
+ KEY_MESSAGES + " INTEGER, " + KEY_TIME_SPENT + " TEXT)";
方法
public Cursor getUserData() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_USER_DATA;
return db.rawQuery(query, null);
}
我在寻找答案并得到一条错误消息后说不存在这样的列,我在其中添加了句段
添加默认数据
public void Start(){
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_MESSAGES, 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();
//-1 means there was an error updating the values
} else {
Toast.makeText(getApplication(),"Done", Toast.LENGTH_SHORT).show();
SharedPreferences sp = getSharedPreferences(MessageRecieverId, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("Data", true); //save that the user enters username
editor.apply();
}
}
像这样添加数据后,我像上面提到的那样将其增加了一个,但显示错误
Process: com.appmaster.akash.messageplus, PID: 24433
android.database.sqlite.SQLiteException: no such column: eR3C1LXJhzaK3fPzCpcuTuz9jZB3 (code 1): , while compiling: UPDATE UserData SET MessagesSent=? WHERE RecieversID =eR3C1LXJhzaK3fPzCpcuTuz9jZB3
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1577)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1525)
at com.appmaster.akash.messageplus.Chat.mMessagesSent(Chat.java:913)
at com.appmaster.akash.messageplus.Chat.sendMessage(Chat.java:704)
at com.appmaster.akash.messageplus.Chat.access$900(Chat.java:75)
at com.appmaster.akash.messageplus.Chat$5.onClick(Chat.java:441)
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)
答案 0 :(得分:1)
UPDATE将更新每一行,除非您指定要更新的行。
long returnVariable2 = db.update(TABLE_USER_DATA, contentValues2,null,null);
由于选择(第3个参数)为 null ,将更新每一行。
选择是一个WHERE子句(减去WHERE关键字);可以使用第4个参数将参数传递到WHERE子句中,以一对一的方式替换?。
例如您最可能想要的东西类似于:-
//contentValues2.put(KEY_ID, uid); //<<<<<<<<<< YOU PROBABLY DO NOT WANT TO CHANGE THE ID, so commented out
contentValues2.put(KEY_MESSAGES_SENT, userData+1);
String whereclause = KEY_ID + "=?";
String[] whereargs = new String[]{uid};
long returnVariable2 = db.update(TABLE_USER_DATA, contentValues2,whereclause,whereargs);
请注意,update返回的int不会很长,并且永远不会为-1,因为它是已更新的行数,因此为0或更大。因此,您需要检查Toast是否为0而不是-1
答案 1 :(得分:1)
您将在10.XX.XX.XXX
的{{1}}调用中专门设置where
子句和where args
参数。这就是该命令更新所有行的原因,因为没有db.update
子句。
您需要更改此行的最后两个参数:
null
这是官方文档中的详细信息:
答案 2 :(得分:0)
ContentValues cv = new ContentValues();
cv.put("isSynced", 1);
db.update(TABLE_NAME, cv, "id =" + recID, null);
使用where条件更新代码。希望它能起作用