问题描述
我是android的新手,我无法理解sqlite数据库中的删除功能.我已使用字符串
I am new to android and i am having trouble understanding the delete function in sqlite database.I have created my database using the string
private static final String TABLE_NAME="NameInfo";
private static final String POSITION_DB ="_id";
private static final String Name_DB ="name";
private static final String JSONOBJECT_DB="json";
private static final String TIMESTAMP_DB="time";
private static final String USERRATING_DB="userrating";
private static final String DATABASE_NAME ="database.db";
private final String createDb ="create table if not exists "+TABLE_NAME +" ("
+ POSITION_DB + " integer primary key, "
+ NAME_DB + " text not null, "
+ JSONOBJECT_DB + " text not null, "
+ USERRATING_DB + " text not null, "
+TIMESTAMP_DB + " text not null); ";
现在,当我启动我的应用程序时,我希望删除超过2天之前添加的所有行
Now when i start my app i want that all rows that were added more than 2 days ago should be deleted
所以我打算做这样的事情
so i am planning to do something like this
long currentdate =new date().getTime();
然后检查表的每一行的currenttime-Long.Valueof(TIMESTAMP_DB)字段之间的差异,如果大于2 * 24 * 60 * 60,则删除该行
and than check the difference between currenttime-Long.Valueof(TIMESTAMP_DB) field for each rows of the table and if it is more than 2*24*60*60 than delete that row
有人可以告诉我如何使用以下功能实现以上功能
Can someone please tell me how can i use the below function to achieve the above
public int delete (String table, String whereClause, String[] whereArgs)
我不确定我应该在whereClause和whereArgs中写什么.
i am not sure what should i write in whereClause and whereArgs.
如果有人能告诉我一个比这更好,更简单的方法,我将非常感激.
I would be really grateful if someone can tell me a even better and simple approach than this.
PS我也尝试通过execSQL语句执行操作,但无法编写完整的查询通过database.execSQL("Delete * from "+TABLE_NAME+" WHERE = "+currentdate - Long.ValueOf(?) >2*24*60*60 ;")
PS i also tried doing by execSQL statement but was not able to write the complete queryby database.execSQL("Delete * from "+TABLE_NAME+" WHERE = "+currentdate - Long.ValueOf(?) >2*24*60*60 ;")
谢谢.
推荐答案
您可以使用如下查询:
db.execSQL("DELETE FROM " + TABLE_NAME +
" WHERE " + currentdate + " - " + TIMESTAMP_DB + " > 2*24*60*60");
对于update
方法,在whereClause
参数的WHERE
子句中编写SQL表达式; whereArgs
仅对于字符串值是必需的:
For the update
method, write the SQL expression in the WHERE
clause in the whereClause
parameter; whereArgs
is needed only for string values:
db.update(TABLE_NAME, currentdate + " - " + TIMESTAMP_DB + " > 2*24*60*60", null);
这篇关于如何删除SQLite数据库中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!