2012-06-28 50 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);