我正在研究一个应用程序,该应用程序从手机中读取联系人姓名,并检查名称是否已保存在数据库中。如果它已经保存到数据库,那么我只是更新一个保存的计数器变量,但如果它不是,那么我继续添加它。至少这是我的意图。代码无法在Android中添加已保存的记录SQLite
部分如下:
public Integer countRecords(String name) {
SQLiteDatabase db = events.getReadableDatabase();
Cursor mCount= db.rawQuery("select count('"+CONTACT_NAME+"') from'"+ TABLE_NAME + "' where '" + CONTACT_NAME + "' = '" + name + "'", null);
mCount.moveToFirst();
Integer count= mCount.getInt(0);
startManagingCursor(mCount);
return count;
}
,并在代码的主体去如下:
ContentResolver cr= getContentResolver();
Cursor cu= cr.query(URI, null, null, null, null);
if(cu.getCount()>0){
while(cu.moveToNext()){
contactName=cu.getString(cu.getColumnIndex(DNAME));
Integer rawValue = countRecords(contactName);
if(rawValue==0){
addRecord(contactName);
addedCounter+=1;
recordName=cu.getString(cu.getColumnIndex(DNAME));
recordInfo = addedCounter + " " + recordName + "\n";
recordsList+= recordInfo;
}
else{
savedCounter+=1;
}
}
现在,我已经试过我知道的一切。问题似乎是程序countRecords
的返回值。也许我在IF子句if(rawValue==0){
中没有使用正确的标准,因为它要么添加所有的联系人,即使它们已经保存在数据库中,或者它没有。
好亚历克斯你是天才毋庸置疑有关!!!!有一件事是在你跪在你面前之前丢失的...... – APOSTOLOS
类型SQLiteDatabase中的方法query(String,String [],String,String [],String,String,String)不适用于参数字符串, 字符串[],字符串,字符串[],空)这个查询中缺少什么? – APOSTOLOS
oops ...你可能会使用任何'query'方法,例如[** this one **](http://tinyurl.com/4hgmrlt)。 –