Advertisement
Guest User

Untitled

a guest
May 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. package com.example.akisha.herbal;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class DatabaseHelper extends SQLiteOpenHelper{
  12. private static final int DB_VERSION = 1;
  13.  
  14. public static final String DB_NAME = "herbal.sqlite";
  15.  
  16. private static DatabaseHelper mInstance = null;
  17.  
  18. private static Context mContext;
  19. private static SQLiteDatabase mDb;
  20.  
  21. public static DatabaseHelper getInstance(Context context) {
  22. if(mInstance == null) {
  23. mInstance = new DatabaseHelper(context.getApplicationContext());
  24. }
  25. return mInstance;
  26. }
  27.  
  28. public DatabaseHelper(Context context) {
  29. super(context, DB_NAME, null, DB_VERSION);
  30. }
  31.  
  32. @Override
  33. public void onCreate(SQLiteDatabase db) {
  34.  
  35. db.execSQL(BookmarkTable.SQL_CREATE_TABLE);
  36. }
  37.  
  38. @Override
  39. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  40.  
  41. }
  42.  
  43. public static void init(Context context) {
  44. mContext = context;
  45. mDb = DatabaseHelper.getInstance(mContext).getWritableDatabase();
  46. }
  47.  
  48. public static void delete(String name){
  49. mDb.delete(BookmarkTable.TABLE_NAME, "Festival = ?", new String[]{name});
  50.  
  51. }
  52.  
  53. public static boolean update(String name, String status){
  54.  
  55. ContentValues update = new ContentValues();
  56.  
  57. update.put(BookmarkTable.BOOKMARK_STATUS, status);
  58.  
  59. int result = mDb.update(BookmarkTable.TABLE_NAME, update, BookmarkTable.BOOKMARK_NAME + " = ?", new String[]{name});
  60.  
  61. if(result > 0){
  62. return true;
  63. }else {
  64. return false;
  65. }
  66.  
  67. }
  68.  
  69. //method for inserting new data.
  70. public static void insert(String tableName, ContentValues contentValues){
  71. mDb.insert(tableName, null, contentValues);
  72. }
  73.  
  74. public List<Bookmarks> getFestival() {
  75. SQLiteDatabase db = this.getReadableDatabase();
  76. Cursor cursor = db.query(BookmarkTable.TABLE_NAME, null, null, null, null, null, null);
  77. List<Bookmarks> accessor = new ArrayList<>();
  78. Bookmarks festival;
  79. if (cursor.getCount() > 0) {
  80. for (int i = 0; i < cursor.getCount(); i++) {
  81. cursor.moveToNext();
  82. festival = new Bookmarks();
  83. festival.setId(cursor.getInt(0));
  84. festival.setName(cursor.getString(1));
  85. festival.setImage(cursor.getBlob(2));
  86. accessor.add(festival);
  87. }
  88. }
  89. cursor.close();
  90. db.close();
  91. return accessor;
  92. }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement