Android的记录存在()的数据库? [英] Android record exists() in database?
本文介绍了Android的记录存在()的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在找最快的和正确的方法来检查,如果一个记录在数据库中存在:
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 aString
but I think it should be aLong
. select 1
is more fast thanselect 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.
这篇关于Android的记录存在()的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文