2012-06-28 21 views
1

我正在研究一个应用程序,该应用程序从手机中读取联系人姓名,并检查名称是否已保存在数据库中。如果它已经保存到数据库,那么我只是更新一个保存的计数器变量,但如果它不是,那么我继续添加它。至少这是我的意图。代码无法在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){中没有使用正确的标准,因为它要么添加所有的联系人,即使它们已经保存在数据库中,或者它没有。

回答

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(); 

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

+0

好亚历克斯你是天才毋庸置疑有关!!!!有一件事是在你跪在你面前之前丢失的...... – APOSTOLOS

+0

类型SQLiteDatabase中的方法query(String,String [],String,String [],String,String,String)不适用于参数字符串, 字符串[],字符串,字符串[],空)这个查询中缺少什么? – APOSTOLOS

+1

oops ...你可能会使用任何'query'方法,例如[** this one **](http://tinyurl.com/4hgmrlt)。 –

0

使用此查询:

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

你有列名之间的额外的单引号。

0

有点所需查询的简单形式:

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