我在我的Android應用程序中構建了一個內部數據庫。 我用它來管理購物車,所以我需要刪除,添加和更新行 Java代碼:Android DatabaseHandler:刪除一行
public class DatabaseCarrello extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "biosfera_carrello";
private static final String TABLE_SESSION = "sessione";
private static final String KEY_ID = "id";
private static final String KEY_ID_PROD = "id_prodotto";
private static final String KEY_ID_USER = "id_user";
private static final String KEY_NOME_PROD = "nome_prodotto";
private static final String KEY_COSTO = "costo";
private static final String KEY_QUANTITA = "quantita";
public DatabaseCarrello(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private static final String DATABASE_CREATE = "create table "
+ TABLE_SESSION + "(" +
KEY_ID_PROD
+ " text not null, "+
KEY_ID_USER
+" text not null, "+
KEY_NOME_PROD
+" text not null, "+
KEY_COSTO
+" text not null, "+
KEY_QUANTITA
+" text not null "+
");";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SESSION);
onCreate(db);
}
public Carrello getCarrello(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_SESSION,new String[] { KEY_ID_PROD, KEY_ID_USER,KEY_NOME_PROD,KEY_COSTO,KEY_QUANTITA }, KEY_ID + "=?",new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Carrello carrello = new Carrello(cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5));
return carrello;
}
public int updateQuantita(Carrello stato) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUANTITA, stato.getQuantita());
return db.update(TABLE_SESSION, values, KEY_ID_PROD + " = ?",
new String[] { String.valueOf(stato.getIDprodotto()) });
}
public void addProdotto(Carrello prodotto){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID_PROD, prodotto.getIDprodotto());
values.put(KEY_ID_USER, prodotto.getIDuser());
values.put(KEY_NOME_PROD, prodotto.getNomeProdotto());
values.put(KEY_QUANTITA, prodotto.getQuantita());
values.put(KEY_COSTO, prodotto.getCosto());
db.insert(TABLE_SESSION, null, values);
db.close();
}
public List<Carrello> getAllCarrello(){
List<Carrello> carrello = new ArrayList<Carrello>();
String selectQuery = "SELECT * FROM " + TABLE_SESSION;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()){
do{
Carrello contact = new Carrello();
contact.setIDuser(cursor.getString(1));
contact.setIDprodotto(cursor.getString(2));
contact.setQuantita(cursor.getString(3));
contact.setCosto(cursor.getString(4));
contact.setNomeProdotto(cursor.getString(5));
carrello.add(contact);
}while (cursor.moveToNext());
}
return carrello;
}
public void deleteAllCarrello(){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_SESSION, null, null);
db.close();
}
public void deleteProdottoCarrello(Carrello carrello) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_SESSION, KEY_ID_PROD + " = ?",
new String[] {carrello.getIDprodotto() });
db.close();
}}
如果我想刪除行,其數據單項我要傳遞? 我試過了:
db.deleteProdottoCarrello(new Carrello(id_prodotto,null,null,null,null));
但它沒有奏效。
你可以發佈你的logcat跟蹤嗎? – Gunaseelan 2013-05-13 14:03:30
LogCat什麼也沒說......沒有錯誤或類似的東西。似乎什麼也沒有發生 – MikeKeepsOnShine 2013-05-13 14:05:44
儘量把'db.delete(TABLE_SESSION,KEY_ID_PROD + 「=?」, 新的String [] {carrello.getIDprodotto()});'這行到try catch塊,並看看會發生什麼。不要忘記在catch塊中打印你的異常 – Gunaseelan 2013-05-13 14:08:13