2013-05-13 52 views
0

我在我的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)); 

但它沒有奏效。

+0

你可以發佈你的logcat跟蹤嗎? – Gunaseelan 2013-05-13 14:03:30

+0

LogCat什麼也沒說......沒有錯誤或類似的東西。似乎什麼也沒有發生 – MikeKeepsOnShine 2013-05-13 14:05:44

+0

儘量把'db.delete(TABLE_SESSION,KEY_ID_PROD + 「=?」, 新的String [] {carrello.getIDprodotto()});'這行到try catch塊,並看看會發生什麼。不要忘記在catch塊中打印你的異常 – Gunaseelan 2013-05-13 14:08:13

回答

0
//public void deleteProdottoCarrello(Carrello carrello) { 
    // SQLiteDatabase db = this.getWritableDatabase(); 
    // int i = carrello.getIDprodotto(); 
    // System.out.println(i); 
    // db.delete(TABLE_SESSION, KEY_ID_PROD + " = ?", 
    //   new String[] { i }); 
    // db.close(); 
//}} 

改變你DATABASE_CREATE串喜歡以下。

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 "+ 
     ");"; 

注意tablename和openbrace之間的空格。 TABLE_SESSION + " (" +這裏。

我希望這會幫助你。

+0

我試過了: 'public void deleteProdottoCarrello(String i){ SQLiteDatabase db = this.getWritableDatabase(); Log.d(「ID」,i); db.delete(TABLE_SESSION,KEY_ID_PROD +「=?」, new String [] {i}); db.close(); }' and 'db.deleteProdottoCarrello(String.valueOf(v.getId()));' 它還沒有工作。 「i」的值是正確的......是否存在打印查詢的方式? – MikeKeepsOnShine 2013-05-16 09:35:58

+1

@MikeKeepsOnShine立即檢查。 – Gunaseelan 2013-05-16 10:01:28