我有一个名为 contacts.db
的数据库,其中有一个名为 TABLE_NAME
的表,其中有一列名为 Contactname
。我想格式化一个查询来检查作为字符串变量名称中的值的联系人姓名是否已保存为表中的记录,如果不是,则我想添加它,否则什么都不做。我写的代码如下:
SQLiteDatabase db = events.getReadableDatabase();
int rawNum = DatabaseUtils.queryNumEntries(db, TABLE_NAME,
"Contactname=?", new String[] {contactName});
if(rawNum==0){
addRecord(contactName);
addedCounter+=1;
}
else{
savedCounter+=1;
}
但应用程序被迫关闭。你能看出查询有什么问题吗?我所做的是查看是否有行,如果行数为 0,则添加联系人。
最佳答案
也许是时候更新您的开发套件了。
代码中使用的带有签名的方法是在 API 级别 11 中引入的:
public static long queryNumEntries (SQLiteDatabase db, String table, String selection, String[] selectionArgs)
API Level 1 只有两个参数的方法:
public static long queryNumEntries (SQLiteDatabase db, String table)
关于Android SQLite查询: "queryNumEntries .. is not applicable for the arguments"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11213195/