我正在研究一個應用程序,該應用程序從手機中讀取聯繫人姓名,並檢查名稱是否已保存在數據庫中。如果它已經保存到數據庫,那麼我只是更新一個保存的計數器變量,但如果它不是,那麼我繼續添加它。至少這是我的意圖。代碼無法在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)。 –