0

我正在我的 Android 应用程序中构建一个内部数据库。我用它来管理购物车,所以我需要删除、添加和更新行 Java 代码:

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

但它没有用。

4

1 回答 1

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 + " (" +这里。

我希望这能帮到您。

于 2013-05-14T16:02:37.777 回答