1

我正在开发一个从手机读取联系人姓名并检查姓名是否已保存在数据库中的应用程序。如果它已经保存到数据库中,那么我只需更新一个保存的计数器变量,但如果不是,那么我继续添加它。至少这是我的意图。

部分代码如下:

 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){,因为它要么添加所有联系人,即使它们已经保存在数据库中,或者它没有。

4

3 回答 3

1

您当前的实现不仅是错误的……而且效率也非常低。尝试这样的事情:

// use as the 2nd argument; otherwise, the cursor will return all information
// associated with the contacts. this is inefficient because you only care
// about the column DNAME in the while loop.
final String[] PROJECTION_CONTACTS = new String[] { DNAME };
final String[] PROJECTION_DATABASE = new String[] { CONTACT_NAME };

// you only need to retrieve this once (dont do it inside the loop)
SQLiteDatabase db = events.getReadableDatabase();

Cursor c = getContentResolver().query(URI, PROJECTION_CONTACTS, null, null, null);

if (c.moveToFirst()) { 
    // then the cursor is not empty

    // compute the columnIndex for "DNAME" only once
    final int col = c.getColumnIndex(DNAME);

    while(c.moveToNext()) {
        // iterate each 
        contactName = c.getString(col);
        Cursor exist = db.query(TABLE_NAME, 
                                PROJECTION_DATABASE, 
                                CONTACT_NAME + " = ?",
                                new String[] { contactName }, 
                                null);

        if (exist.moveToFirst()) {
            // the cursor is not empty, so it exists in the database
        } else {
            // the cursor is empty, so it doesn't exist in the database
        }
        exist.close();
    }
}
c.close();

像这样的东西应该工作。(我向你保证我在某处打错了字,但它的总体思路应该让你开始)。请确保您在单独的线程上异步执行此操作......这可能是一个非常耗时的操作。

于 2012-06-28T15:25:20.657 回答
0

使用此查询:

Cursor mCount= db.rawQuery("select count("+CONTACT_NAME+") from "+ TABLE_NAME + " where " + CONTACT_NAME + " = '" + name + "'", null);

您在列名之间有一个额外的单引号。

于 2012-06-28T14:53:38.537 回答
0

您所需查询的一种简单形式:

Cursor mCount= db.rawQuery("select count(1) from "+ TABLE_NAME + 
" where " + CONTACT_NAME + " = '" + name + "'", null); 
于 2012-06-28T16:23:31.223 回答