public List<Product> getProductsCategory(String categoryId) { List<Product> list = new ArrayList<Product>(); SQLiteDatabase db = null; Cursor cursor = null; try { db = mDbHelper.getReadableDatabase(); cursor = db.query( DatabaseHelper.TABLE_PRODUCTS, mAllColumns, DatabaseHelper.TABLE_PRODUCT_CATEGORY_ID + "=?", new String[] {categoryId}, null, null, null, null); } catch (NullPointerException e) { if (AlwarshaApp.DEBUG) Log.e(TAG, "Exception at getProductGroup"); } if (cursor.moveToFirst()) { for (int i = 0; i < cursor.getCount(); i++) { Product product = null; int db_id = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_ID)); String db_name_ar = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_NAME_AR)); String db_name_en = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_NAME)); int db_category_id = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_CAT_ID)); String db_pic_name = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_PIC_NAME)); float db_price = cursor.getFloat(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_PRICE)); product = new Product(db_id, db_name_en, db_category_id, db_pic_name, db_price, "EN"); product.addName("AR", db_name_ar); list.add(product); cursor.moveToNext(); } } cursor.close(); return list; }
public Product getProduct(int id) { Product product = null; SQLiteDatabase db = null; Cursor cursor = null; try { db = mDbHelper.getReadableDatabase(); cursor = db.query( DatabaseHelper.TABLE_PRODUCTS, mAllColumns, DatabaseHelper.TABLE_PRODUCT_ID + "=?", new String[] {String.valueOf(id)}, null, null, null, null); } catch (NullPointerException e) { if (AlwarshaApp.DEBUG) Log.e(TAG, "Exception at getProduct"); } if (cursor.moveToFirst()) { int db_id = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_ID)); String db_name_ar = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_NAME_AR)); String db_name_en = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_NAME)); int db_category_id = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_CAT_ID)); String db_pic_name = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_PIC_NAME)); float db_price = cursor.getFloat(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_PRICE)); cursor.close(); product = new Product(db_id, db_name_en, db_category_id, db_pic_name, db_price, "EN"); product.addName("AR", db_name_ar); } return product; }
public void insertNewProduct(Product product) { SQLiteDatabase db1 = mDbHelper.getReadableDatabase(); Cursor cursor = db1.query( DatabaseHelper.TABLE_PRODUCTS, mAllColumns, DatabaseHelper.TABLE_PRODUCT_ID + "= ?", new String[] {String.valueOf(product.getmId())}, null, null, null); if (cursor != null && cursor.getCount() != 0) { return; } SQLiteDatabase db = mDbHelper.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(DatabaseHelper.TABLE_PRODUCT_ID, product.getmId()); String name_en = product.getmName("EN"); if (name_en != null) { cv.put(DatabaseHelper.TABLE_PRODUCT_NAME, name_en); } String name_ar = product.getmName("AR"); if (name_ar != null) { cv.put(DatabaseHelper.TABLE_PRODUCT_NAME_AR, name_ar); } cv.put(DatabaseHelper.TABLE_PRODUCT_CATEGORY_ID, product.getmCategoryId()); cv.put(DatabaseHelper.TABLE_PRODUCT_PIC_NAME, product.getmPictureName()); cv.put(DatabaseHelper.TABLE_PRODUCT_PRICE, product.getmPrice()); long i = db.insert(DatabaseHelper.TABLE_PRODUCTS, null, cv); Log.d(TAG, "insert return value = " + i); }