Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - Android sqlite update table always return 0

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;

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()

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...