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));
しかし、うまくいきませんでした。