简体   繁体   English

在Android中,为什么不更新SQLite数据库?

[英]In Android, why won't the SQLite Database update?

I'm attempting to update a row using: 我正在尝试使用以下方法更新行:

myDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=" + mName, null);

but I get the following error: 但我收到以下错误:

ERROR/Database(282): Error updating persons_name=Bob using UPDATE peopleTable SET persons_name=? 错误/数据库(282):使用UPDATE peopleTable SET person_name =?更新person_name = Bob时出错。 WHERE persons_name=Bob WHERE person_name =鲍勃

I am using the following code to attempt the update: 我正在使用以下代码尝试更新:

public void updateEntry(String mName) throws SQLException {

    ContentValues cvUpdate = new ContentValues();
    cvUpdate.put(KEY_NAME, mName);
    ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=" + mName, null);

}

I have looked at other answers which seem to be similar to this problem but I have not found something definitive that solves the problem which I am having. 我看过其他似乎与这个问题相似的答案,但是我没有找到确定的解决方案。 It appears to be something to do with the variables but I may be completely wrong.. 这似乎与变量有关,但我可能完全错了。

You're not enclosing your string in quotes. 您没有将字符串用引号引起来。 You're better off doing something like this: 您最好这样做:

ourDatabase.update(DATEABASE_TABLE, cvUpdate, KEY_NAME + "= ?", new String[]{mName});

when you use the above, it will automatically enclose the param in single-quotes if the type is a string. 当您使用上述代码时,如果类型为字符串,它将自动将参数括在单引号中。

“ persons_name = Bob”可能应该是“ persons_name ='Bob'”,因此请尝试:

ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "='" + mName + "'", null);

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

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