SenpaiZero

SqliteHelper

Jun 3rd, 2024
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. package com.example.a07act;
  2.  
  3. import android.app.Activity;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteConstraintException;
  8. import android.database.sqlite.SQLiteDatabase;
  9. import android.database.sqlite.SQLiteOpenHelper;
  10. import android.util.Log;
  11.  
  12. import java.util.Arrays;
  13. import java.util.LinkedList;
  14.  
  15. public class SqliteHelper extends SQLiteOpenHelper {
  16. private static final int DATABASE_VERSION = 1;
  17. private static final String DATABASE_NAME = "Activity.db";
  18. private static final String FLAVORS_CREATE = "CREATE TABLE IF NOT EXISTS Flavors (" +
  19. "ItemCode INTEGER," + // INTEGER = int
  20. "ItemDesc TEXT," + // TEXT = varchar
  21. "Price REAL" + // REAL = float/double
  22. ");";
  23.  
  24. public SqliteHelper(Context context) {
  25. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  26. }
  27. @Override
  28. public void onCreate(SQLiteDatabase db) {
  29. db.execSQL(FLAVORS_CREATE);
  30. }
  31.  
  32. @Override
  33. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  34. db.execSQL("DROP TABLE IF EXISTS Flavors");
  35. onCreate(db);
  36. }
  37.  
  38. public static class Queries {
  39. Activity activity;
  40. SQLiteDatabase db;
  41. SqliteHelper sqliteHelper;
  42. public Queries(Activity activity) {
  43. this.activity = activity;
  44. sqliteHelper = new SqliteHelper(activity);
  45. db = sqliteHelper.getWritableDatabase();
  46.  
  47. // Para lagi reset data kada restart
  48. db.execSQL("DROP TABLE IF EXISTS Flavors");
  49. db.execSQL(FLAVORS_CREATE);
  50. }
  51.  
  52.  
  53. public boolean populateData(ContentValues values) {
  54. return db.insert("Flavors", null, values) != -1;
  55. }
  56.  
  57. public String[] retrieveItemCode() {
  58. String val = "";
  59. Cursor cursor = db.query(
  60. "Flavors",
  61. new String[] {"ItemCode"},
  62. null,
  63. null,
  64. null,
  65. null,
  66. null
  67. );
  68.  
  69. while(cursor.moveToNext()) {
  70. val += cursor.getInt(cursor.getColumnIndexOrThrow("ItemCode")) + ":";
  71. }
  72.  
  73. return val.split(":");
  74. }
  75.  
  76. public LinkedList<String> retrieveAllCol() {
  77. LinkedList<String> val = new LinkedList<>();
  78. Cursor cursor = db.query(
  79. "Flavors",
  80. new String[] {"ItemCode", "ItemDesc", "Price"},
  81. null,
  82. null,
  83. null,
  84. null,
  85. null
  86. );
  87.  
  88. while (cursor.moveToNext()) {
  89. String[] data = new String[3];
  90. data[0] = String.valueOf(cursor.getInt(cursor.getColumnIndexOrThrow("ItemCode")));
  91. data[1] = cursor.getString(cursor.getColumnIndexOrThrow("ItemDesc"));
  92. data[2] = String.valueOf(cursor.getDouble(cursor.getColumnIndexOrThrow("Price")));
  93.  
  94. String join = Arrays.toString(data);
  95. val.add(join.substring(1, join.length()-1));
  96. }
  97.  
  98. return val;
  99. }
  100.  
  101. public LinkedList<String> retrieveAllCol(double price) {
  102. LinkedList<String> val = new LinkedList<>();
  103. Cursor cursor = db.query(
  104. "Flavors",
  105. new String[] {"ItemCode", "ItemDesc", "Price"},
  106. "Price > ?",
  107. new String[]{String.valueOf(price)},
  108. null,
  109. null,
  110. null
  111. );
  112.  
  113. while (cursor.moveToNext()) {
  114. String[] data = new String[3];
  115. data[0] = String.valueOf(cursor.getInt(cursor.getColumnIndexOrThrow("ItemCode")));
  116. data[1] = cursor.getString(cursor.getColumnIndexOrThrow("ItemDesc"));
  117. data[2] = String.valueOf(cursor.getDouble(cursor.getColumnIndexOrThrow("Price")));
  118.  
  119. String join = Arrays.toString(data);
  120. val.add(join.substring(1, join.length()-1));
  121. }
  122.  
  123. return val;
  124. }
  125.  
  126. public boolean changePrice(int itemCode, double newPrice) {
  127. ContentValues values = new ContentValues();
  128. values.put("Price", newPrice);
  129.  
  130. String selection = "ItemCode = ?";
  131. String[] selectionArgs = {String.valueOf(itemCode)};
  132.  
  133. return db.update(
  134. "Flavors",
  135. values,
  136. selection,
  137. selectionArgs
  138. ) > 0;
  139. }
  140. }
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment