Java Android sqlite更新表总是返回0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22051097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:16:11  来源:igfitidea点击:

Android sqlite update table always return 0

javaandroidsqlite

提问by Haris

I have seen several questions here related to updating in sqlite, but not able to solve this problem. I am unable to update table, it always give 0;

我在这里看到了几个与 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);

}

I am able to insert and fetch data, but there is only problem in updating it.

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

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;
}

P.S. I have also tried execSql()

PS 我也试过 execSql()

采纳答案by Haris

It sounds like nothing matches your criteria. Perhaps as a test you should pass null as your where criteria (this signifies update all rows) and see if 0 is still returned.

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

If 0 is still returned after that I would look to ensure you actually have data saved in your database table.

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

回答by n1k1ch

SQLiteDatabase.update()returns number of rows affected. So, in your case, no rows were affected (most probably because of your WHEREclause)

SQLiteDatabase.update()返回受影响的行数。所以,在你的情况下,没有行受到影响(很可能是因为你的WHERE条款)