2010-11-04 62 views
9

我想编写一个查询来从表中删除一行。我很困惑写这份声明。我需要一些帮助来写这个。我在这里用纯sql语句提供我的需求。Android中的Sqlite删除查询语法

(伪代码)

delete from tablename where value =="string1" && value2 == "string2" && value3 == "string3"; 

我想为Android db.delete(tablename, whereclause, whereargs); SQLite的语法来写这篇文章。

我会感谢您的帮助。

回答

15
String table_name = "myTable"; 
String where = "value1 = 'string1'" 
    + " AND value2 = 'string2'" 
    + " AND value3 = 'string3'"; 
String whereArgs = null; 
mDb.delete(table_name, where, whereArgs); 
+0

感谢您的答复。我打算使用它。我还有一个问题,如果我想提供整数。我应该直接将整数变量直接放置在'string2'位置。 – Balakrishna 2010-11-05 20:06:21

+0

如果“value2”是整数,那么你可以写“value2 = 1”。如果“value2”是TEXT,那么你必须写“value2 ='1'” – user479211 2010-11-08 07:35:27

28

一个更好的办法是使用

String where = "value1 = ?" 
+ " AND value2 = ?" 
+ " AND value3 = ?"; 
String[] whereArgs = {string1,string2,string3}; 
2
mdb.delete("tablename",new String("value1=? and Value2=? and value3=?"),new String[]{value1,value2,value3}); 
1
SQLiteDatabase db = this.getWritableDatabase(); 
db.execSQL("DELETE FROM tablename WHERE value1='"+string1+"' AND value2='"+string2+"' AND value3='"+string3+"' "); //delete row in a table with the condition 
db.close();