Android插入/更新/删除SQLite查询

And*_*oid 1 sqlite android

我正在尝试编写三种不同的方法来在Android中的SQLite数据库中插入,更新和删除数据.到目前为止,我可以在数据库中插入数据,但我无法理解如何where在SQL中添加该子句.这是我正在使用的代码:

更新方法:

public boolean updateSQL(String tableName,String key,String value){
    return updateData(tableName,key,value);
}

private static boolean updateData(String tableName,String key,String value){
    sqliteDb = instance.getWritableDatabase();
    String where = "id=?";
    ContentValues values = new ContentValues();
    values.put(key, value);
    values.put(key, value);
    sqliteDb.update(tableName, values, where, null);
    return true;
}
Run Code Online (Sandbox Code Playgroud)

...我正在调用这样的方法:

dbHelper.updateSQL("saved_codes", "code_id", "3");
//dbHelper is an instance to a custom DatabaseHelper class.
Run Code Online (Sandbox Code Playgroud)

..和删除方法:

public boolean deleteSQL(String tableName,String key,String value){
    return deleteData(tableName,key,value);
}

private static boolean deleteData(String tableName,String key,String value) {
    sqliteDb = instance.getWritableDatabase();
    ContentValues values = new ContentValues();
    String where = null;
    String[] whereArgs = null;
    values.put(key, value);
    values.put(key, value);
    sqliteDb.delete(tableName, where, whereArgs);
    return true;
}
Run Code Online (Sandbox Code Playgroud)

我知道,字符串wherewhereArgsnull,但实际上我不明白如何添加他们.我不希望有人为我编写代码,但欢迎来自互联网的一些好建议,建议甚至样本.

njz*_*zk2 5

你需要whereArgs

String where = "id=?";
Run Code Online (Sandbox Code Playgroud)

就像是 :

sqliteDb.update(tableName, values, where, new String[] {"42"});
Run Code Online (Sandbox Code Playgroud)

其中42将是要更新的行的_id.也更喜欢BaseColumns._ID到"id".