1
public void addFavourite(User user,Winkel winkel){
ContentValues values = new ContentValues();
values.put(COLUMN_NAME,winkel.getNaam());
values.put(COLUMN_POSTCODE, winkel.getPostcode());
values.put(COLUMN_DEELGEMEENTE, winkel.getDeelGemeente());
values.put(COLUMN_GEMEENTE, winkel.getGemeente());
values.put(COLUMN_ADRES, winkel.getAdres());
Log.d("lol",user.getUserName()); // Log is succesfull
values.put(COLUMN_USERNAME, user.getUserName());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_FAVOURITES, null, values);
db.close();
}
public void deleteFavourite(User user,Winkel winkel){
String selectQuery = "DELETE FROM " + TABLE_FAVOURITES + " WHERE " + COLUMN_USERNAME+"=" + user.getUserName() + " AND " + COLUMN_NAME + "=" + winkel.getNaam();
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(selectQuery);
db.close();
}
String CREATE_FAVOURITES_TABLE = "CREATE TABLE IF NOT EXISTS " +
TABLE_FAVOURITES + "("
+ COLUMN_ID2 + " INTEGER PRIMARY KEY,"
+ COLUMN_NAME + " TEXT,"
+ COLUMN_POSTCODE + " INTEGER,"
+ COLUMN_DEELGEMEENTE + " TEXT,"
+ COLUMN_GEMEENTE + " TEXT,"
+ COLUMN_ADRES + " TEXT,"
+ COLUMN_USERNAME + " TEXT, "
+ "FOREIGN KEY(" + COLUMN_USERNAME+ ") REFERENCES "
+ TABLE_USERS + "(username) " + ")";
db.execSQL(CREATE_FAVOURITES_TABLE);
試圖刪除收藏時
SQL異常:SQLlite - 在DELETE命令沒有這樣的列
android.database.sqlite.SQLiteException: no such column: Gebruikersnaam (code 1): , while compiling: DELETE FROM favorieten WHERE username=Gebruikersnaam AND naam=EUROCITY
注:所以我假設要麼添加喜歡的什麼是錯的,而實際上用戶名沒有被添加到表中,或者在create table語句中出現錯誤,或者在我的deletequery中出現錯誤。我無法弄清楚。爲什麼這不起作用?
您需要附上字符串單引號。 I.e .:'WHERE username ='Gebruikersnaam'AND naam ='EUROCITY'' –
你忘了引用你的價值。但實際上你應該使用Prepared Statements –
@juergend是的,綁定的參數效果更好。在包含一個或多個撇號的字符串的情況下,他們還會考慮將單引號加倍(''''>'''') –