atultiwari

DBHelper.java

Jul 16th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. // --------- DBHelper.java -----//
  2. package com.atultiwari.medicalquiz;
  3.  
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import android.content.Context;
  12. import android.database.Cursor;
  13. import android.database.SQLException;
  14. import android.database.sqlite.SQLiteDatabase;
  15. import android.database.sqlite.SQLiteException;
  16. import android.database.sqlite.SQLiteOpenHelper;
  17.  
  18.  
  19.  
  20. /**
  21.  * @author robert.hinds
  22.  *
  23.  */
  24. public class DBHelper extends SQLiteOpenHelper{
  25.  
  26.     //The Android's default system path of your application database.
  27.     private static String DB_PATH = "/data/data/com.atultiwari.medicalquiz/databases/";
  28.     private static String DB_NAME = "questionsDb";
  29.     private SQLiteDatabase myDataBase;
  30.     private final Context myContext;
  31.  
  32.     /**
  33.      * Constructor
  34.      * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
  35.      * @param context
  36.      */
  37.     public DBHelper(Context context) {
  38.         super(context, DB_NAME, null, 1);
  39.         this.myContext = context;
  40.     }  
  41.  
  42.     /**
  43.      * Creates a empty database on the system and rewrites it with your own database.
  44.      * */
  45.     public void createDataBase() throws IOException{
  46.  
  47.         boolean dbExist = checkDataBase();
  48.         if(!dbExist)
  49.         {
  50.             //By calling this method and empty database will be created into the default system path
  51.             //of your application so we are gonna be able to overwrite that database with our database.
  52.             this.getReadableDatabase();
  53.  
  54.             try {
  55.                 copyDataBase();
  56.             } catch (IOException e) {
  57.                 throw new Error("Error copying database");
  58.             }
  59.         }
  60.     }
  61.  
  62.     /**
  63.      * Check if the database already exist to avoid re-copying the file each time you open the application.
  64.      * @return true if it exists, false if it doesn't
  65.      */
  66.     private boolean checkDataBase(){
  67.         SQLiteDatabase checkDB = null;
  68.         try{
  69.             String myPath = DB_PATH + DB_NAME;
  70.             checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  71.         }catch(SQLiteException e){
  72.             //database does't exist yet.
  73.         }
  74.         if(checkDB != null){
  75.             checkDB.close();
  76.         }
  77.  
  78.         return checkDB != null ? true : false;
  79.     }
  80.  
  81.     /**
  82.      * Copies your database from your local assets-folder to the just created empty database in the
  83.      * system folder, from where it can be accessed and handled.
  84.      * This is done by transfering bytestream.
  85.      * */
  86.     private void copyDataBase() throws IOException{
  87.  
  88.         //Open your local db as the input stream
  89.         InputStream myInput = myContext.getAssets().open(DB_NAME);
  90.  
  91.         // Path to the just created empty db
  92.         String outFileName = DB_PATH + DB_NAME;
  93.  
  94.         //Open the empty db as the output stream
  95.         OutputStream myOutput = new FileOutputStream(outFileName);
  96.  
  97.         //transfer bytes from the inputfile to the outputfile
  98.         byte[] buffer = new byte[1024];
  99.         int length;
  100.         while ((length = myInput.read(buffer))>0){
  101.             myOutput.write(buffer, 0, length);
  102.         }
  103.  
  104.         //Close the streams
  105.         myOutput.flush();
  106.         myOutput.close();
  107.         myInput.close();
  108.  
  109.     }
  110.  
  111.     public void openDataBase() throws SQLException{
  112.         //Open the database
  113.         String myPath = DB_PATH + DB_NAME;
  114.         myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  115.     }
  116.  
  117.     @Override
  118.     public synchronized void close() {
  119.         if(myDataBase != null)
  120.             myDataBase.close();
  121.         super.close();
  122.     }
  123.  
  124.     @Override
  125.     public void onCreate(SQLiteDatabase db) {
  126.     }
  127.  
  128.     @Override
  129.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  130.     }
  131.  
  132.     // Add your public helper methods to access and get content from the database.
  133.     // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
  134.     // to you to create adapters for your views.
  135.  
  136.    
  137.    
  138.    
  139.     public List<Question> getQuestionSet(int difficulty, int numQ){
  140.         List<Question> questionSet = new ArrayList<Question>();
  141.         Cursor c = myDataBase.rawQuery("SELECT * FROM questions WHERE difficulty=" + difficulty +
  142.                 " ORDER BY RANDOM() LIMIT " + numQ, null);
  143.         while (c.moveToNext()){
  144.             //Log.d("QUESTION", "Question Found in DB: " + c.getString(1));
  145.             Question q = new Question();
  146.             q.setQuestion(c.getString(1));
  147.             q.setAnswer(c.getString(2));
  148.             q.setOption1(c.getString(3));
  149.             q.setOption2(c.getString(4));
  150.             q.setOption3(c.getString(5));
  151.             q.setRating(difficulty);
  152.             questionSet.add(q);
  153.         }
  154.         return questionSet;
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment