/** getting all products */ public List<Product> getAllProducts() { List<Product> products = new ArrayList<Product>(); String selectQuery = "SELECT * FROM " + TABLE_PRODUCTS; Log.e(LOG, selectQuery); SQLiteDatabase db = this.getReadableDatabase(); Cursor c = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (c.moveToFirst()) { do { Product pd = new Product(); pd.setId(c.getInt((c.getColumnIndex(KEY_ID)))); pd.setName((c.getString(c.getColumnIndex(KEY_NAME)))); pd.setCode((c.getString(c.getColumnIndex(KEY_CODE)))); pd.setImage((c.getString(c.getColumnIndex(KEY_IMAGEN)))); pd.setCompany_id((c.getInt(c.getColumnIndex(KEY_COMPANY_ID)))); pd.setCategory_id((c.getInt(c.getColumnIndex(KEY_CATEGORY_ID)))); pd.setCategory_name((c.getString(c.getColumnIndex(KEY_CATEGORY_NAME)))); // adding to todo list products.add(pd); } while (c.moveToNext()); } return products; }
/** * @param name * @return */ public Product getProductName(String name) { SQLiteDatabase db = this.getReadableDatabase(); String selectQuery = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE " + KEY_NAME + " = " + name; Log.e(LOG, selectQuery); Cursor c = db.rawQuery(selectQuery, null); if (c != null) c.moveToFirst(); Product pd = new Product(); pd.setId(c.getInt(c.getColumnIndex(KEY_ID))); pd.setName((c.getString(c.getColumnIndex(KEY_NAME)))); pd.setCode((c.getString(c.getColumnIndex(KEY_CODE)))); pd.setImage((c.getString(c.getColumnIndex(KEY_IMAGEN)))); pd.setCompany_id((c.getInt(c.getColumnIndex(KEY_COMPANY_ID)))); pd.setCategory_id((c.getInt(c.getColumnIndex(KEY_CATEGORY_ID)))); pd.setCategory_name((c.getString(c.getColumnIndex(KEY_CATEGORY_NAME)))); return pd; }
/* * Updating a User */ public int updateProduct(Product product) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, product.getName()); values.put(KEY_CODE, product.getCode()); values.put(KEY_COMPANY_ID, product.getCompany_id()); values.put(KEY_IMAGEN, product.getImage()); values.put(KEY_CATEGORY_ID, product.getCategory_id()); values.put(KEY_CATEGORY_NAME, product.getCategory_name()); // updating row return db.update( TABLE_PRODUCTS, values, KEY_ID + " = ?", new String[] {String.valueOf(product.getId())}); }
/* * Creating a PRODUCTS */ public long createProduct(Product product) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_ID, product.getId()); values.put(KEY_NAME, product.getName()); values.put(KEY_CODE, product.getCode()); values.put(KEY_IMAGEN, product.getImage()); values.put(KEY_COMPANY_ID, product.getCompany_id()); values.put(KEY_CATEGORY_ID, product.getCategory_id()); values.put(KEY_CATEGORY_NAME, product.getCategory_name()); // insert row // long todo_id = db.insert(TABLE_PEDIDO, null, values); db.insert(TABLE_PRODUCTS, null, values); long todo_id = product.getId(); return todo_id; }