I'm trying to get the old QTY Record and add to it the new value, I want you to know that the qty is a varchar in the database , so I created a method to convert it to integer
this is DataBAseHelper Methods
public void checkIfExcist(Context mCtx, String recordName,
String ValueCheck, String changedQTY, String changedPrice,
String changedPNAme) {
DatabaseHelper mDbHelper = new DatabaseHelper(mCtx);
SQLiteDatabase mDb = mDbHelper.getWritableDatabase();
Cursor mCursor = mDb.rawQuery("SELECT * FROM " + SQLITE_TABLE
+ " WHERE " + recordName + " ='" + ValueCheck + "'", null);
if (mCursor != null && mCursor.moveToFirst()) {
Toast.makeText(mCtx, "Available", 3000).show();
ContentValues values = new ContentValues();
values.put(qty, changedQTY);
values.put(price, changedPrice);
mDb.update(SQLITE_TABLE, values, PRODUCT_NAME + "='" + changedPNAme
+ "'", null);
} else {
Toast.makeText(mCtx, "Not Available", 3000).show();
/* record not exist */
}
}
public int getQuantity(int Quantity, String pName, String tableName,
String myQTY) {
String theQTY = "nothing";
String selectQuery = "SELECT " + pName + "FROM " + tableName
+ " WHERE " + myQTY + "='" + Quantity + "';";
Cursor c = mDb.rawQuery(selectQuery, new String[] { "hello.txt" });
if (c.moveToFirst()) {
theQTY = c.getString(c.getColumnIndex(pName));
}
c.close();
int convertedQTY = Integer.parseInt(theQTY);
return Quantity + convertedQTY;
}
and this is how I use the methods in the class onClick Method
int myQTY = Integer.parseInt(qty.getText().toString());
String myTitle= m.getTitle().toString();
String myCategory = m.getCategory().toString();
int myCategoryN = m.getOrderNum();
double myPrice = m.getPrice()*myQTY;
int newMyQTY = myADapterDB.getQuantity(myQTY, "productName", "Country", "myqty");
myADapterDB.checkIfExcist(activity, "productName", myTitle, newMyQTY+"", myPrice+"", myTitle);
Aucun commentaire:
Enregistrer un commentaire