2014-03-19 152 views
0

我嘗試只在電話號碼(號碼)不存在的情況下將一些值(消息正文和日期)插入數據庫,但我的代碼無法正常工作。你能提出一些建議嗎?Android/SQLite。通過過濾在數據庫中插入多個值?

SQLiteDatabase db = this.getWritableDatabase(); 


    Cursor cursor = db.rawQuery("INSERT INTO version2 (number, body, date) values ("+number+","+body+","+ date+") SELECT "+number+" WHERE NOT EXISTS " 
      + "(SELECT 1 FROM version2 WHERE number = "+number+")", null); 

    if (cursor.moveToFirst()) { 
     do { 

     } while (cursor.moveToNext()); 

    } 
} 

回答

0

試試這個查詢。

似乎您的選擇查詢在插入記錄時不正確。

INSERT INTO version2 (number, body, date) values ("+number+","+body+","+ date+") WHERE number IS NOT NULL or number <> ''; 
0

您需要創建一個方法來檢查重複的電話號碼。

private boolean isPhoneExists (long number) 
{ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery("select phone from version2 where number = " + number, null); 

    if (cursor.getCount() > 0) 
    return true; 
    else 
    return false; 
} 

//現在你插入語句

if (!isPhoneExists ("987654321")) 
{ 
     Cursor cursor = db.rawQuery("INSERT INTO version2 (number, body, date) values ("+number+","+body+","+ date+") 
    .... 
}