Advertisement
IrakliKardava

Untitled

Feb 7th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.List;
  3.  
  4.  
  5.  
  6. import android.content.ContentValues;
  7. import android.content.Context;
  8. import android.database.Cursor;
  9. import android.database.sqlite.SQLiteDatabase;
  10. import android.database.sqlite.SQLiteOpenHelper;
  11. import android.util.Log;
  12.  
  13. public class MySQLiteHelper extends SQLiteOpenHelper {
  14.  
  15.     // Database Version
  16.     private static final int DATABASE_VERSION = 1;
  17.     // Database Name
  18.     private static final String DATABASE_NAME = "MedicineDb";
  19.  
  20.     public MySQLiteHelper(Context context) {
  21.         super(context, DATABASE_NAME, null, DATABASE_VERSION);  
  22.     }
  23.  
  24.     @Override
  25.     public void onCreate(SQLiteDatabase db) {
  26.         // SQL statement to create medicine table
  27.         String CREATE_BOOK_TABLE = "CREATE TABLE medicines ( " +
  28.                 "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
  29.                 "title TEXT )";
  30.  
  31.         // create medicine table
  32.         db.execSQL(CREATE_MEDICINNE_TABLE);
  33.     }
  34.  
  35.     @Override
  36.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  37.         // Drop older medicine table if existed
  38.         db.execSQL("DROP TABLE IF EXISTS medicines");
  39.  
  40.         // create fresh medicine table
  41.         this.onCreate(db);
  42.     }
  43.     //----------------------------------------------------------
  44. public Medicine getMedicineList(int id){
  45.  
  46.         // 1. get reference to readable DB
  47.         SQLiteDatabase db = this.getReadableDatabase();
  48.  
  49.         // 2. build query
  50.         Cursor cursor =
  51.                 db.query(TABLE_MEDICINS, // a. table
  52.                 COLUMNS, // b. column names
  53.                 " id = ?", // c. selections
  54.                 new String[] { String.valueOf(id) }, // d. selections args
  55.                 null, // e. group by
  56.                 null, // f. having
  57.                 null, // g. order by
  58.                 null); // h. limit
  59.  
  60.         // 3. if we got results get the first one
  61.         if (cursor != null)
  62.             cursor.moveToFirst();
  63.  
  64.         // 4. build medicine object
  65.         Medicine medicine = new Medicine();
  66.         medicine.setId(Integer.parseInt(cursor.getString(0)));
  67.         medicine.setTitle(cursor.getString(1));
  68.      
  69.  
  70.         Log.d("getMedicineList("+id+")", medicine.toString());
  71.  
  72.         // 5. return medicine
  73.         return medicine;
  74.     }
  75.  
  76.     // Get All medicines
  77.     public List<Medicine> getAllMedicines() {
  78.         List<Medicins> medicines = new LinkedList<Medicine>();
  79.  
  80.         // 1. build the query
  81.         String query = "SELECT  * FROM " + TABLE_MEDICINS;
  82.  
  83.         // 2. get reference to writable DB
  84.         SQLiteDatabase db = this.getWritableDatabase();
  85.         Cursor cursor = db.rawQuery(query, null);
  86.  
  87.         // 3. go over each row, build medicine and add it to list
  88.         Medicine medicine = null;
  89.         if (cursor.moveToFirst()) {
  90.             do {
  91.                 medicine = new Medicine();
  92.                 medicine.setId(Integer.parseInt(cursor.getString(0)));
  93.                 medicine.setTitle(cursor.getString(1));
  94.            
  95.  
  96.                 // Add medicine to medicines
  97.                 medicines.add(medicine);
  98.             } while (cursor.moveToNext());
  99.         }
  100.  
  101.         Log.d("getMedicineList()", medicines.toString());
  102.  
  103.         // return medicines
  104.         return medicines;
  105.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement