我正在研究购物车模块。当我按下购物车按钮时,我想将购物车添加到我的数据库中,场景是我的详细信息页面中有两个产品,一个是电子书,另一个是 pbook。如果我按下其中任何一个,我需要根据 pro、name 和 auto increment 键创建一行。然后,如果我按下第二个购物车按钮,我需要通过检查 pro 或 name 值的存在来再次更新同一行。以下是我创建数据库的方式。如果数据库列中存在该值,任何人都可以帮助我如何更新该行。
//////From activity
AddCartHelper databaseHelper=new AddCartHelper(WYWProductDetail.this);
databaseHelper.saveCategoryRecord(wyw_pro_id,wyw_book_name);
////Helper Class
public class AddCartHelper {
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "category.db";
private static final String TABLE_NAME = "tbcategory";
public static final String CATEGORY_COLUMN_ID = "_id";
public static final String CATEGORY_COLUMN_PRO = "pro";
public static final String CATEGORY_COLUMN_NAME = "name";
Category openHelper;
private SQLiteDatabase database;
ContentValues contentValues;
String name_id;
public AddCartHelper(Context context) {
openHelper = new Category(context);
database = openHelper.getWritableDatabase();
}
public void saveCategoryRecord(String name, String pro) {
contentValues = new ContentValues();
contentValues.put(CATEGORY_COLUMN_PRO, pro);
contentValues.put(CATEGORY_COLUMN_NAME, name);
if(!name.equals(contentValues.get(CATEGORY_COLUMN_NAME))) {
} else {
database.insert(TABLE_NAME, null, contentValues);
database.close();
}
}
public Cursor getTimeRecordList() {
return database.rawQuery("select * from " + TABLE_NAME, null);
}
private class Category extends SQLiteOpenHelper {
public Category(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME + "( "
+ CATEGORY_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ CATEGORY_COLUMN_PRO + " TEXT, "
+ CATEGORY_COLUMN_NAME + " TEXT )" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
onCreate(db);
}
}
}