简体   繁体   English

Android SQLite 更新表无返回

[英]Android SQLite Update Table no return

  • When First Insert data it Works当第一次插入数据时它工作
  • But When I update Query it doesn't work但是当我更新查询时它不起作用

Here is my code what I'm trying to do.这是我正在尝试做的代码。

fun updateData(mData: Data) {
            if (!databaseOpen) {
                database = INSTANCE.writableDatabase
                databaseOpen = true
            }
  val values = ContentValues()

values.put(DatabaseConstan.USER_ID, mData.userId)
values.put(DatabaseConstan.ABOUT, mData.userInfo.about)


return database.update(DatabaseConstan.DATABASE_TABEL, values, "${DatabaseConstan.USER_ID}=${mData.userId}", null);

But this method works但是这种方法有效

 return  database.execSQL("UPDATE data SET userId=${mData.userId} WHERE id='1'");

Here is Create Table Command这是创建表命令

 val QUERY_CREATE = "CREATE TABLE  $DATABASE_TABEL ($ROW_ID INTEGER PRIMARY KEY AUTOINCREMENT, $USER_ID INTEGER , $ABOUT TEXT)"

I'm confused and trying but con't understand我很困惑和尝试但不明白

The mData object contains the new values of 3 columns that you want to replace the old values. mData object 包含要替换旧值的 3 列的新值。
So mData.userId contains the new userId that does not exist in the table.因此mData.userId包含表中不存在的新userId
This makes the WHERE clause of the UPDATE statement, which is the 3d argument of the method update() : "${DatabaseConstan.USER_ID}=${mData.userId}" to return nothing.这使得UPDATE语句的WHERE子句,即方法update()的 3d 参数: "${DatabaseConstan.USER_ID}=${mData.userId}"不返回任何内容。

So instead of ${mData.userId} you must use the original value of the column which you must save in a variable like currentUserId and change to:因此,您必须使用必须保存在currentUserId等变量中的列的原始值,而不是${mData.userId}并更改为:

"${DatabaseConstan.USER_ID}=$currentUserId"

Or maybe you want to set the WHERE clause so you search by id and not by userId and assuming that the mData object contains the id also:或者,也许您想设置WHERE子句,以便按id而不是按userId搜索,并假设mData object 也包含id

"${DatabaseConstan.ID}=${mData.Id}"

Change the names of the variables to your actual names.将变量的名称更改为您的实际名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM