Android sqlite更新表总是返回0

Har*_*ris 9 java sqlite android

我在这里看到了几个与sqlite更新有关的问题,但是无法解决这个问题.我无法更新表,它总是给0;

public void onCreate(SQLiteDatabase db) {
    try{
    db.execSQL(createTable);
    db.execSQL(createQuestionTable);
    populateQuestionTable(db);
    }

    catch( SQLException e)
    {
        e.printStackTrace();
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS alarms" );
    onCreate(db);

}
Run Code Online (Sandbox Code Playgroud)

我能够插入和获取数据,但更新它只有问题.

public int updateTable( int r )
{
    String row = String.valueOf(r);
    Log.d("mydb", "disbale alarm" + " " + row);
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(isDisable, 1);

    int result = 0;
    try{
     result = db.update("alarms", values,  keyId+" = ? " ,new String[]{row});
    Log.d("result", ""+result);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    db.close();

    return result;
}
Run Code Online (Sandbox Code Playgroud)

PS我也试过execSql()

小智 12

听起来没有什么符合您的标准.也许作为测试,您应该将null作为where条件传递(这表示更新所有行)并查看是否仍返回0.

如果之后仍然返回0,我会确保您确实在数据库表中保存了数据.


n1k*_*1ch 7

SQLiteDatabase.update()返回受影响的行数.因此,在您的情况下,没有行受到影响(很可能是因为您的WHERE子句)