Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.a07act;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteConstraintException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- import java.util.Arrays;
- import java.util.LinkedList;
- public class SqliteHelper extends SQLiteOpenHelper {
- private static final int DATABASE_VERSION = 1;
- private static final String DATABASE_NAME = "Activity.db";
- private static final String FLAVORS_CREATE = "CREATE TABLE IF NOT EXISTS Flavors (" +
- "ItemCode INTEGER," + // INTEGER = int
- "ItemDesc TEXT," + // TEXT = varchar
- "Price REAL" + // REAL = float/double
- ");";
- public SqliteHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(FLAVORS_CREATE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS Flavors");
- onCreate(db);
- }
- public static class Queries {
- Activity activity;
- SQLiteDatabase db;
- SqliteHelper sqliteHelper;
- public Queries(Activity activity) {
- this.activity = activity;
- sqliteHelper = new SqliteHelper(activity);
- db = sqliteHelper.getWritableDatabase();
- // Para lagi reset data kada restart
- db.execSQL("DROP TABLE IF EXISTS Flavors");
- db.execSQL(FLAVORS_CREATE);
- }
- public boolean populateData(ContentValues values) {
- return db.insert("Flavors", null, values) != -1;
- }
- public String[] retrieveItemCode() {
- String val = "";
- Cursor cursor = db.query(
- "Flavors",
- new String[] {"ItemCode"},
- null,
- null,
- null,
- null,
- null
- );
- while(cursor.moveToNext()) {
- val += cursor.getInt(cursor.getColumnIndexOrThrow("ItemCode")) + ":";
- }
- return val.split(":");
- }
- public LinkedList<String> retrieveAllCol() {
- LinkedList<String> val = new LinkedList<>();
- Cursor cursor = db.query(
- "Flavors",
- new String[] {"ItemCode", "ItemDesc", "Price"},
- null,
- null,
- null,
- null,
- null
- );
- while (cursor.moveToNext()) {
- String[] data = new String[3];
- data[0] = String.valueOf(cursor.getInt(cursor.getColumnIndexOrThrow("ItemCode")));
- data[1] = cursor.getString(cursor.getColumnIndexOrThrow("ItemDesc"));
- data[2] = String.valueOf(cursor.getDouble(cursor.getColumnIndexOrThrow("Price")));
- String join = Arrays.toString(data);
- val.add(join.substring(1, join.length()-1));
- }
- return val;
- }
- public LinkedList<String> retrieveAllCol(double price) {
- LinkedList<String> val = new LinkedList<>();
- Cursor cursor = db.query(
- "Flavors",
- new String[] {"ItemCode", "ItemDesc", "Price"},
- "Price > ?",
- new String[]{String.valueOf(price)},
- null,
- null,
- null
- );
- while (cursor.moveToNext()) {
- String[] data = new String[3];
- data[0] = String.valueOf(cursor.getInt(cursor.getColumnIndexOrThrow("ItemCode")));
- data[1] = cursor.getString(cursor.getColumnIndexOrThrow("ItemDesc"));
- data[2] = String.valueOf(cursor.getDouble(cursor.getColumnIndexOrThrow("Price")));
- String join = Arrays.toString(data);
- val.add(join.substring(1, join.length()-1));
- }
- return val;
- }
- public boolean changePrice(int itemCode, double newPrice) {
- ContentValues values = new ContentValues();
- values.put("Price", newPrice);
- String selection = "ItemCode = ?";
- String[] selectionArgs = {String.valueOf(itemCode)};
- return db.update(
- "Flavors",
- values,
- selection,
- selectionArgs
- ) > 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment