简体   繁体   English

尝试在Android中的sqlite中更新最后一条记录中的某个列

[英]Trying to update a certain column in the last record in sqlite in Android

I have a db that is structred as such我有一个这样结构的数据库

uid | username | password |     email   | cur_level
1     default      abc123   me@email.com    0

In my game after you complete a level in the main activity I run a method called increase_current_level() value++;在我的游戏中,当您完成主要活动中的一个关卡后,我会运行一个名为increase_current_level() value++; 的方法; test_db.open(); test_db.open(); test_db.updateLevel(value); test_db.updateLevel(value); cur_level = value; cur_level = 值; test_db.close(); test_db.close();

So that seems to work with no problems but when test_db.updateLevel(value) is run, that when the program crashes.所以这似乎没有问题,但是当test_db.updateLevel(value)运行时,当程序崩溃时。

In my DBHelper the code for update level is as follows.在我的 DBHelper 中,更新级别的代码如下。

public boolean updateLevel(String level) {
        ContentValues args = new ContentValues();

        args.put(KEY_CUR_LEVEL, level);
        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "="
                + "WHERE id=(SELECT max(id) FROM DATABASE_TABLE)", null) > 0;

    }

My log cat error seems to be:我的日志猫错误似乎是:

10-17 17:36:10.833: E/AndroidRuntime(1815): Caused by: android.database.sqlite.SQLiteException: near "WHERE": syntax error (code 1): , while compiling: UPDATE test SET cur_level=? WHERE _id=WHERE id=(SELECT max(id) FROM DATABASE_TABLE)

I'm very new to SQLite so, it's kind of intimidating, but I hope someone sees a very obvious flaw.我对 SQLite 很陌生,所以有点吓人,但我希望有人能看到一个非常明显的缺陷。 Thanks谢谢

Update 1:更新 1:

My updated code:我更新的代码:

public boolean updateLevel(String level) {
    ContentValues args = new ContentValues();

    args.put(KEY_CUR_LEVEL, level);
    return mDb.update(DATABASE_TABLE, args, KEY_ROWID + " = (SELECT max(id) FROM DATABASE_TABLE",
            null) > 0;

}

Updated error msg:更新错误消息:

10-17 18:26:50.804: E/AndroidRuntime(3684): Caused by: android.database.sqlite.SQLiteException: near "DATABASE_TABLE": syntax error (code 1): , while compiling: UPDATE test SET cur_level=? WHERE _id = (SELECT max(id) FROM DATABASE_TABLE

From your code, your WHERE clause is probably malformed.从您的代码来看,您的 WHERE 子句可能格式错误。 Shouldn't it be不应该是

"WHERE id=(SELECT max(id) FROM " + DATABASE_TABLE

instead of代替

"WHERE id=(SELECT max(id) FROM DATABASE_TABLE)"

? ?

The syntax of mDb.update is mDb.update的语法是

update(table, ContentValues values, String whereClause, String[] whereArgs)

Let's assume that KEY_ROWID = "id"让我们假设KEY_ROWID = "id"

For your whereClause , you're literally passing对于您的whereClause ,您实际上是在传递

"id = WHERE id = (SELECT max(id) FROM DATABASE_TABLE"

which is not valid SQL.这不是有效的 SQL。

You don't seem to need " id = ".您似乎不需要“ id = ”。 Just start your WHERE clause with " WHERE id= "只需以“ WHERE id= ”开始您的WHERE子句

However, your question implies that you have only one record in the DB.但是,您的问题意味着您在数据库中只有一条记录。 Why use a DB for one record?为什么要为一条记录使用数据库? And if you have more than one record in it, you should use a WHERE clause that searches on something unique, rather than using SELECT max(id) .如果其中有多个记录,则应使用WHERE子句搜索唯一的内容,而不是使用SELECT max(id)

The bottom line is that you need to improve your knowledge of SQL.最重要的是,您需要提高对 SQL 的了解。

The update function already puts a WHERE into the query, and you have duplicated the column name ( KEY_ROWID and "id" ). update函数已经将WHERE放入查询中,并且您已经复制了列名( KEY_ROWID"id" )。 Use:用:

mDb.update(..., KEY_ROWID + "= (SELECT ...)");

If your table only has one row then you don't need a WHERE clause.如果您的表只有一行,那么您不需要 WHERE 子句。 The following statement should update all rows in your table.以下语句应更新表中的所有行。 If only one record exists this will solve your problem.如果只有一个记录存在,这将解决您的问题。

return mDb.update(DATABASE_TABLE, args, null, null) > 0;返回 mDb.update(DATABASE_TABLE, args, null, null) > 0;

If more than one row exists you will need to know the uid of the row you want to update prior to calling the update function.如果存在多于一行,您将需要在调用更新函数之前知道要更新的行的 uid。 In which case it would look like this...在这种情况下,它看起来像这样......

String dbID = "the unique id of the row you want to update"; String dbID = "要更新的行的唯一 ID";

return mDb.update(DATABASE_TABLE, args, "id=?", new String[]{dbID}) > 0;返回 mDb.update(DATABASE_TABLE, args, "id=?", new String[]{dbID}) > 0;

Hope this helps.希望这可以帮助。

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

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