Update table using rawQuery() method does not work

2020-02-05 09:51发布

I tried the following SQLite query:

    int idServizo = 150;
    String whereClause = id_servizio+" = '"+idServizio+" ' ";
    ContentValues cv = new ContentValues();
    cv.put("sync", 1);

    int r = dbManager.updateTable("myTable", cv, whereClause);

Where fields sync and id_servizio are both integer. The method updateTable is:

 public int updateTable(String table, ContentValues values, String whereClause){
    int r = mDb.update(table, values, whereClause, null);
    return r;
}

 // mDb is SQLiteDatabase object

All this works good. But if I try this with the rawQuery() method:

 public Cursor RawQuery(String sqlQuery, String[] columns){
    return mDb.rawQuery(sqlQuery, columns);    
}

The table is not updated! even if no error occurs.

 int idServizo = 150;
 String updateQuery ="UPDATE myTable SET sync = 1 WHERE id_servizio = "+idServizio;
 dbManager.RawQuery(updateQuery, null);

Why does this not work?

5条回答
Summer. ? 凉城
2楼-- · 2020-02-05 10:24

Your update call formats the ID as string, while the rawQuery call formats is as number.

Assuming that the ID in the table indeed is a string, use:

String updateQuery = "UPDATE myTable SET sync = 1 WHERE id_servizio = '" + idServizio + "'";
查看更多
叛逆
3楼-- · 2020-02-05 10:25

Instead of doing this:

Cursor c= dbManager.RawQuery(updateQuery, null);
c.moveToFirst();
c.close();

You just need this:

dbManager.execSQL(updateQuery, null);

----------------------------------------------------------------------------

Posting answer because sometimes many people (like me) not reading comments. Most popular answer is not correct but Yaqub Ahmad's comment is correct.

Answer from CommonsWare explained in this answer:

rawQuery() is for SQL statements that return a result set. Use execSQL() for SQL statements, like INSERT, that do not return a result set.

----------------------------------------------------------------------------

Documentation for execSQL:

public void execSQL (String sql)

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

Documentation for rawQuery:

public Cursor rawQuery (String sql, 
                String[] selectionArgs)

Runs the provided SQL and returns a Cursor over the result set.

查看更多
你好瞎i
4楼-- · 2020-02-05 10:28

This is because when a rawQuery is executed cursor is returned. Without the call to cursor.moveToFirst() and cursor.close() the database won't get updated.

int idServizo = 150;``
String updateQuery ="UPDATE myTable SET sync = 1 WHERE id_servizio = "+idServizio;
Cursor c= dbManager.RawQuery(updateQuery, null);

c.moveToFirst();
c.close();

I dont know the need to call moveToFirst() but this works fine and the database gets updated.

查看更多
冷血范
5楼-- · 2020-02-05 10:30

Problem solved.

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

Can't works because rawQuery runs the provided SQL and returns a Cursor over the result set.

If I want to return a table I have to use rawQuery, otherwise no!

Increase the value of a record in android/sqlite database

查看更多
淡お忘
6楼-- · 2020-02-05 10:44

You should use db.execSQL() instead db.rawQuery().

查看更多
登录 后发表回答