2016-07-07 152 views
-1

此代碼只刪除我表中的單個記錄,我想要實現的是刪除具有給定字符串電話值的所有記錄。 Im新的SQlite,一點幫助,非常感謝。先謝謝你。SQlite刪除所有具有特定字符串值的行

此代碼成功地刪除記錄單:

public void delSmsData(int id){ 
    String deleteQuery = "DELETE FROM "+TABLE_LO_SMSDATA+" WHERE _id="+id+""; 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.execSQL(deleteQuery); //delete single row by ID in a table 
    db.execSQL("vacuum"); 
    db.close(); 
    Log.e("Debug", "Successful delete of message with id no. " + id); 
} 

我successfuly刪除所有記錄,此代碼。感謝那些幫助過的人。

public void delAllSmsByPhone(String phone){ 
    String deleteQuery = "DELETE FROM "+TABLE_LO_SMSDATA+" WHERE _phone='"+phone+"'"; 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.execSQL(deleteQuery); 
    db.execSQL("vacuum"); 
    db.close(); 
} 

這是我的表結構:

String CREATE_LO_SMSDATA_TABLE = "CREATE TABLE " 
      +TABLE_LO_SMSDATA+"(" 
      +KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT," 
      +KEY_EMAIL+" TEXT," 
      +KEY_TARGETNAME+" TEXT," 
      +KEY_SMSTYPE+" TEXT," 
      +KEY_PHONE+" TEXT," 
      +KEY_CONTACTNAME+" TEXT," 
      +KEY_SMSBODY+" TEXT," 
      +KEY_DATE+" TEXT"+")"; 
    db.execSQL(CREATE_LO_SMSDATA_TABLE); 
+0

什麼是你的表結構? '_phone'是主鍵嗎? –

+0

@AdamForbis我更新了這個問題。 – Draeko

+1

'... WHERE _phone ='「+ phone +」'「' –

回答

1

我不完全理解你。您是否嘗試運行代碼?

此代碼是不正確的做法

這絕對是正確的做法。 DELETE可以刪除任何數量的行,而不僅僅是一個。它將刪除WHERE子句爲真的所有行。

證明:

 
sqlite> .tables 
sms 
sqlite> .schema sms 
CREATE TABLE `sms` (
    `_id` INTEGER PRIMARY KEY AUTOINCREMENT, 
    `_phone` TEXT 
); 
sqlite> SELECT * FROM sms; 
1|+79271234567 
2|+79271234567 
3|+79213983722 
4|+79271234567 
5|+78450738422 
6|+79271234567 
7|+77432739417 
8|+74673627246 
9|+79271234567 
10|+79271234567 
sqlite> DELETE FROM sms WHERE _phone='+79271234567'; 
sqlite> SELECT * FROM sms; 
3|+79213983722 
5|+78450738422 
7|+77432739417 
8|+74673627246 
+0

是的,我忘記了,對不起,我的手機已經加密了,謝謝... WHERE _phone ='」+ phone +「'」。這個。 – Draeko

相關問題