Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.marek.mojalistaproduktow;
- import android.annotation.SuppressLint;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import java.text.Format;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- /**
- * Created by Marek on 05.04.2018.
- */
- public class DatabaseHelper extends SQLiteOpenHelper {
- private static final int DATABASE_VERSION = 1;
- private static final String DATABASE_NAME = "productmanager.db";
- private static final String TABLE_PRODUCT = "product";
- private static final String COLUMN_PRODUCT_NAME = "product_name";
- private static final String COLUMN_PRODUCT_DESCRIPTION = "product_description";
- private static final String COLUMN_PRODUCT_EANCODE = "product_eancode";
- private static final String TABLE_PRICES = "prices";
- private static final String COLUMN_PRICE = "price";
- private static final String COLUMN_DATE = "date";
- private static final String CREATE_TABLE_PRODUCT =
- "CREATE TABLE " + TABLE_PRODUCT + "("
- + COLUMN_PRODUCT_EANCODE + " TEXT PRIMARY KEY NOT NULL UNIQUE, "
- + COLUMN_PRODUCT_NAME + " TEXT, "
- + COLUMN_PRODUCT_DESCRIPTION + " TEXT)";
- private static final String CREATE_TABLE_PRICES =
- "CREATE TABLE " + TABLE_PRICES + "("
- + COLUMN_PRODUCT_EANCODE + " TEXT, "
- + COLUMN_PRICE + " DOUBLE, "
- + COLUMN_DATE + " DATETIME)";
- private static final String DROP_TABLE_PRODUCT =
- "DROP TABLE IF EXISTS " + TABLE_PRODUCT;
- private static final String DROP_TABLE_PRICES =
- "DROP TABLE IF EXISTS " + TABLE_PRICES;
- public DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(CREATE_TABLE_PRODUCT);
- db.execSQL(CREATE_TABLE_PRICES);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int i, int i1) {
- db.execSQL(DROP_TABLE_PRODUCT);
- db.execSQL(DROP_TABLE_PRICES);
- onCreate(db);
- }
- public boolean addProduct(Product product) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues contentValues = new ContentValues();
- contentValues.put(COLUMN_PRODUCT_EANCODE, product.getEanCode());
- contentValues.put(COLUMN_PRODUCT_NAME, product.getName());
- contentValues.put(COLUMN_PRODUCT_DESCRIPTION, product.getDescription());
- long result = db.insert(TABLE_PRODUCT, null, contentValues);
- if(result == -1)
- return false;
- else {
- ContentValues values = new ContentValues();
- for(int i=0; i<product.getPricesCount(); ++i) {
- values.put(COLUMN_PRODUCT_EANCODE, product.getEanCode());
- values.put(COLUMN_PRICE, product.getPrice(i).getPrice());
- @SuppressLint("SimpleDateFormat")
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String date = formatter.format(product.getPrice(0).getDate());
- values.put(COLUMN_DATE, date);
- }
- result = db.insert(TABLE_PRICES, null, values);
- if(result == -1)
- return false;
- }
- return true;
- }
- public boolean addPrice(String eanCode, Price price) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues contentValues = new ContentValues();
- contentValues.put(COLUMN_PRODUCT_EANCODE, eanCode);
- contentValues.put(COLUMN_PRICE, price.getPrice());
- @SuppressLint("SimpleDateFormat")
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- contentValues.put(COLUMN_DATE, formatter.format(price.getDate()));
- long result = db.insert(TABLE_PRICES, null, contentValues);
- if(result == -1)
- return false;
- return true;
- }
- public boolean isExist(String eanCode) {
- String[] selectionArgs = { eanCode };
- String selection = COLUMN_PRODUCT_EANCODE + " = ?";
- SQLiteDatabase db = this.getWritableDatabase();
- Cursor cursor = db.query(TABLE_PRODUCT,
- null,
- selection,
- selectionArgs,
- null,
- null,
- null);
- int cursorCount = cursor.getCount();
- if(cursorCount > 0)
- return true;
- return false;
- }
- public Product getData(String eanCode) {
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor result = db.rawQuery("SELECT * FROM " + TABLE_PRODUCT +
- " WHERE " + COLUMN_PRODUCT_EANCODE + " = " + eanCode,
- null);
- if(result != null) {
- result.moveToFirst();
- Product product = new Product(result.getString(result.getColumnIndex(COLUMN_PRODUCT_NAME)),
- result.getString(result.getColumnIndex(COLUMN_PRODUCT_EANCODE)),
- result.getString(result.getColumnIndex(COLUMN_PRODUCT_DESCRIPTION)),
- null);
- return product;
- }
- else
- return null;
- }
- public ArrayList<Price> getPrices(String eancode) {
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor result = db.rawQuery("SELECT * FROM " + TABLE_PRICES + " WHERE " + COLUMN_PRODUCT_EANCODE + " LIKE " + eancode, null);
- result.moveToFirst();
- ArrayList<Price> prices = new ArrayList<>();
- do {
- String dateString = result.getString(result.getColumnIndex(COLUMN_DATE));
- double price = result.getDouble(result.getColumnIndex(COLUMN_PRICE));
- Date date = null;
- try {
- date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateString);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- prices.add(new Price(price, date));
- } while(result.moveToNext());
- return prices;
- }
- public Cursor getProducts() {
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor result = db.rawQuery("SELECT * FROM " + TABLE_PRODUCT,null);
- return result;
- }
- public void editProduct(Product product) {
- SQLiteDatabase db = this.getWritableDatabase();
- String query = "UPDATE " + TABLE_PRODUCT + " SET " + COLUMN_PRODUCT_NAME + " = '" + product.getName() + "', "
- + COLUMN_PRODUCT_DESCRIPTION + " = '" + product.getDescription() + "'"
- + " WHERE " + COLUMN_PRODUCT_EANCODE + " = '" + product.getEanCode() + "';";
- db.execSQL(query);
- }
- public void deleteAllDate() {
- SQLiteDatabase db = this.getReadableDatabase();
- db.execSQL("DELETE FROM " + TABLE_PRODUCT);
- db.execSQL("DELETE FROM " + TABLE_PRICES);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement