由网友(心之所向便是光)分享简介:我在找最快的和正确的方法来检查,如果一个记录在数据库中存在:I am looking to the fastest and the correct way to check if a record exists in the database:public boolean Exists(String _id) {C...
我在找最快的和正确的方法来检查,如果一个记录在数据库中存在:
I am looking to the fastest and the correct way to check if a record exists in the database:
public boolean Exists(String _id) {
Cursor c=db.query(TABLENAME(), new String[] {"1"}, "_ID="+_id, null, null, null, null);
if (!c.equals(null))
return c.moveToFirst();
return false;
}
你看不出什么问题呢?
Do you see any problem with it?
推荐答案
考虑到 MDB
是 SqlLiteDatabase
类
public boolean Exists(String _id) {
Cursor cursor = mDb.rawQuery("select 1 from yourTable where _id=%s",
new String[] { _id })
boolean exists = (cursor.getCount() > 0);
cursor.close();
return exists;
}
在我把你的参数 _id
为字符串
,但我认为它应该是一个龙
。
选择1
比更快选择COLUMNNAME
,因为这个过程不需要检索的值表的SELECT子句。
您可以将字符串选择1,从...
在静态最终常数甚至更快。
I keep your parameter _id
as a String
but I think it should be a Long
.
select 1
is more fast than select columnName
because the process doesn't need the retrieve a value from the table in the select clause.
you can put the string select 1 from...
in a static final constant to be even faster.
相关推荐
最新文章