Reputation: 371
I am working on shopping cart module. I want to add the cart to my database when i am pressing cart button the scenario is i have two product in my detail page one is ebook and another one is pbook. If i press either one of them i need to create a row based on pro , name and auto increment key. Then if i press second cart button i need to update the same row once again by checking the pro or name value existence. Below is how i created the database. Can anyone help me how to update the row if the value exists in database column.
//////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);
}
}
}
Upvotes: 1
Views: 8141
Reputation: 6179
This will update all rows whose Name = oldName with newName.
public int updateAllName(String oldName, String newName) {
ContentValues values = new ContentValues();
values.put(CATEGORY_COLUMN_NAME, newName);
return Db.update(TABLE_NAME, values, CATEGORY_COLUMN_NAME + " = '" + oldName + "'", null);
}
This will update all rows whose Pro = oldPro with newPro
public int updateAllPro(String oldPro, String newPro) {
ContentValues values = new ContentValues();
values.put(CATEGORY_COLUMN_PRO, newPro);
return Db.update(TABLE_NAME, values, CATEGORY_COLUMN_PRO + " = '" + oldPro + "'", null);
}
Upvotes: 3