Guest User

DbHelper

a guest
Feb 3rd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.69 KB | None | 0 0
  1. package com.example.tung.flagquiz.DbHelper;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteException;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9.  
  10. import com.example.tung.flagquiz.Model.Question;
  11. import com.example.tung.flagquiz.Model.Ranking;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.io.OutputStream;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20.  
  21. /**
  22.  * Created by Tung on 1/27/2017.
  23.  */
  24.  
  25. public class DbHelper extends SQLiteOpenHelper {
  26.     private static String DB_NAME = "FlagDB.db";
  27.     private static String DB_PATH = "";
  28.     private SQLiteDatabase mDataBase;
  29.     private Context mContext = null;
  30.  
  31.     public DbHelper(Context context) {
  32.         super(context, DB_NAME, null, 1);
  33.         DB_PATH = context.getApplicationInfo().dataDir + "/databases";
  34.         File file = new File(DB_PATH+"FlagDB.db");
  35.         if(file.exists())
  36.             openDatabase();
  37.         this.mContext = context;
  38.     }
  39.  
  40.     public void openDatabase() {
  41.         String myPath = DB_PATH + DB_NAME;
  42.         mDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
  43.     }
  44.  
  45.     public void copyDatabase() throws IOException {
  46.         try {
  47.             InputStream myInput = mContext.getAssets().open(DB_NAME);
  48.             String outputFileName = DB_PATH + DB_NAME;
  49.             OutputStream myOutput = new FileOutputStream(outputFileName);
  50.  
  51.             byte[] buffer = new byte[1024];
  52.             int length;
  53.             while ((length = myInput.read(buffer)) > 0) ;
  54.             myOutput.write(buffer, 0, length);
  55.  
  56.             myOutput.flush();
  57.             myOutput.close();
  58.             myInput.close();
  59.         } catch (Exception e) {
  60.             e.printStackTrace();
  61.  
  62.         }
  63.     }
  64.  
  65.  
  66.     private boolean checkDataBase() {
  67.         SQLiteDatabase tempDB = null;
  68.         try {
  69.             String myPath = DB_PATH + DB_NAME;
  70.             tempDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
  71.         } catch (SQLiteException e) {
  72.             e.printStackTrace();
  73.         }
  74.         if (tempDB != null)
  75.             tempDB.close();
  76.         return tempDB != null ? true : false;
  77.     }
  78.  
  79.     public void createDataBase() throws IOException {
  80.         boolean isDBExists = checkDataBase();
  81.         if (isDBExists) {
  82.  
  83.         } else {
  84.             this.getReadableDatabase();
  85.             try {
  86.                 copyDatabase();
  87.             } catch (IOException e) {
  88.                 e.printStackTrace();
  89.             }
  90.         }
  91.  
  92.     }
  93.  
  94.     @Override
  95.     public synchronized void close() {
  96.         if (mDataBase != null)
  97.             mDataBase.close();
  98.  
  99.         super.close();
  100.     }
  101.  
  102.     @Override
  103.     public void onCreate(SQLiteDatabase db) {
  104.  
  105.     }
  106.  
  107.     @Override
  108.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  109.  
  110.     }
  111.  
  112.     public List<Question> getAllQuestion() {
  113.         List<Question> listQuestion = new ArrayList<>();
  114.         SQLiteDatabase db = this.getWritableDatabase();
  115.         Cursor c;
  116.         try {
  117.             c = db.rawQuery("SELECT * FROM Question ORDER BY Random()", null);
  118.             if (c == null) return null;
  119.             c.moveToFirst();
  120.             do {
  121.                 int Id = c.getInt(c.getColumnIndex("ID"));
  122.                 String Image = c.getString(c.getColumnIndex("Image"));
  123.                 String answerA = c.getString(c.getColumnIndex("AnswerA"));
  124.                 String answerB = c.getString(c.getColumnIndex("AnswerB"));
  125.                 String answerC = c.getString(c.getColumnIndex("AnswerC"));
  126.                 String answerD = c.getString(c.getColumnIndex("AnswerD"));
  127.                 String correctAnswer = c.getString(c.getColumnIndex("CorrectAnswer"));
  128.                 String traloi1 = answerA.toUpperCase();
  129.                 String traloi2 = answerB.toUpperCase();
  130.                 String traloi3 = answerC.toUpperCase();
  131.                 String traloi4 = answerD.toUpperCase();
  132.                 String traloidung = correctAnswer.toUpperCase();
  133.                 Question question = new Question(Id, Image, traloi1.toUpperCase(), traloi2.toUpperCase(), traloi3.toUpperCase(), traloi4.toUpperCase(), traloidung.toUpperCase());
  134.                 listQuestion.add(question);
  135.  
  136.             }
  137.             while (c.moveToNext());
  138.             c.close();
  139.         } catch (Exception e) {
  140.             e.printStackTrace();
  141.         }
  142.         db.close();
  143.         return listQuestion;
  144.  
  145.  
  146.     }
  147.  
  148.  
  149.  
  150.     public void InsertScore(int Score){
  151.         SQLiteDatabase db = this.getWritableDatabase();
  152.         ContentValues contentValues = new ContentValues();
  153.         contentValues.put("Score",Score);
  154.         db.insert("Ranking",null,contentValues);
  155.     }
  156.  
  157.     public List<Ranking> getHighScores(){
  158.         List<Ranking> listHighScores = new ArrayList<>();
  159.         SQLiteDatabase db = this.getWritableDatabase();
  160.         Cursor c;
  161.         try{
  162.             c=db.rawQuery("SELECT * FROM High Scores ORDER BY Scores DESC;",null);
  163.             if (c==null) return null;
  164.             c.moveToNext();
  165.             do{
  166.                 int Id = c.getInt(c.getColumnIndex("ID"));
  167.                 int Score = c.getInt(c.getColumnIndex("Score"));
  168.                 Ranking ranking = new Ranking(Id,Score);
  169.                 listHighScores.add(ranking);
  170.             }
  171.             while(c.moveToNext());
  172.             c.close();
  173.         }
  174.         catch (Exception e){
  175.             e.printStackTrace();
  176.         }
  177.         db.close();
  178.         return listHighScores;
  179.     }
  180. }
Add Comment
Please, Sign In to add comment