Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

android - Update table using rawQuery() method does not work

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...