SQLite增加不起作用

时间:2016-07-28 02:03:15

标签: android sql database sqlite

我正在尝试使用以下内容增加名为“attempts”的列的值:

db.execSQL("UPDATE " + DataProviderContract.STORAGE.TABLE_NAME + " SET "
                + DataProviderContract.STORAGE.TRIES + " = " + DataProviderContract.STORAGE.TRIES + " +1 WHERE "
                + DataProviderContract.STORAGE.HASHNAME + " = '" +hashName+"'");

这是修改前后的Cursor Dump:

07-27 22:58:05.579 11573-11838/com.a.s D/ImgManagement: STORAGE DUMP BEFORE: >>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@75c4874
                                                                             0 {
                                                                                hashname=24969a3f1710cd838c6c1eecd10d31b9b5188a3a4209fb8b2e337a0a26b4dc27
                                                                                type=0
                                                                                date=1469671085
                                                                                tries=0
                                                                             }
                                                                             <<<<<

    07-27 22:58:05.589 11573-11838/com.a.s D/ImgManagement: STORAGE DUMP AFTER: >>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@75c4874
                                                                             0 {
                                                                                hashname=24969a3f1710cd838c6c1eecd10d31b9b5188a3a4209fb8b2e337a0a26b4dc27
                                                                                type=0
                                                                                date=1469671085
                                                                                tries=0
                                                                             }

如你所见,没有区别。我甚至记录了传递给db.execSql的字符串,这就是它:

    07-27 22:58:05.587 11573-11838/com.a.s D/ImgManagement: UPDATE storage SET tries = tries +1 WHERE hashname = '24969a3f1710cd838c6c1eecd10d31b9b5188a3a4209fb8b2e337a0a26b4dc27'

有人知道什么是错的?我一直试图做几个小时,但找不到问题。

1 个答案:

答案 0 :(得分:0)

SQLiteDatabase.execSQL()执行单个SQL语句,该语句 NOT SELECT或任何其他返回数据的SQL语句

SQLiteDatabase.update()只是一种方便的方法,用于更新数据库中的行,这些行采用内部使用SQLiteStatement来完成其工作的ContentValues中的值。

绑定参数避免引用或转义值。

然后,你应该使用:

final SQLiteStatement stmt = db.compileStatement("UPDATE " +       
    DataProviderContract.STORAGE.TABLE_NAME + " SET " +
    DataProviderContract.STORAGE.TRIES + " = " +  
    DataProviderContract.STORAGE.TRIES + " +1 WHERE " +
    DataProviderContract.STORAGE.HASHNAME + " = ?");
stmt.bindString(1, hashName);
final int rows = stmt.executeUpdateDelete();