Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.tung.flagquiz.DbHelper;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteException;
- import android.database.sqlite.SQLiteOpenHelper;
- import com.example.tung.flagquiz.Model.Question;
- import com.example.tung.flagquiz.Model.Ranking;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by Tung on 1/27/2017.
- */
- public class DbHelper extends SQLiteOpenHelper {
- private static String DB_NAME = "FlagDB.db";
- private static String DB_PATH = "";
- private SQLiteDatabase mDataBase;
- private Context mContext = null;
- public DbHelper(Context context) {
- super(context, DB_NAME, null, 1);
- DB_PATH = context.getApplicationInfo().dataDir + "/databases";
- File file = new File(DB_PATH+"FlagDB.db");
- if(file.exists())
- openDatabase();
- this.mContext = context;
- }
- public void openDatabase() {
- String myPath = DB_PATH + DB_NAME;
- mDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
- }
- public void copyDatabase() throws IOException {
- try {
- InputStream myInput = mContext.getAssets().open(DB_NAME);
- String outputFileName = DB_PATH + DB_NAME;
- OutputStream myOutput = new FileOutputStream(outputFileName);
- byte[] buffer = new byte[1024];
- int length;
- while ((length = myInput.read(buffer)) > 0) ;
- myOutput.write(buffer, 0, length);
- myOutput.flush();
- myOutput.close();
- myInput.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private boolean checkDataBase() {
- SQLiteDatabase tempDB = null;
- try {
- String myPath = DB_PATH + DB_NAME;
- tempDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
- } catch (SQLiteException e) {
- e.printStackTrace();
- }
- if (tempDB != null)
- tempDB.close();
- return tempDB != null ? true : false;
- }
- public void createDataBase() throws IOException {
- boolean isDBExists = checkDataBase();
- if (isDBExists) {
- } else {
- this.getReadableDatabase();
- try {
- copyDatabase();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public synchronized void close() {
- if (mDataBase != null)
- mDataBase.close();
- super.close();
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- public List<Question> getAllQuestion() {
- List<Question> listQuestion = new ArrayList<>();
- SQLiteDatabase db = this.getWritableDatabase();
- Cursor c;
- try {
- c = db.rawQuery("SELECT * FROM Question ORDER BY Random()", null);
- if (c == null) return null;
- c.moveToFirst();
- do {
- int Id = c.getInt(c.getColumnIndex("ID"));
- String Image = c.getString(c.getColumnIndex("Image"));
- String answerA = c.getString(c.getColumnIndex("AnswerA"));
- String answerB = c.getString(c.getColumnIndex("AnswerB"));
- String answerC = c.getString(c.getColumnIndex("AnswerC"));
- String answerD = c.getString(c.getColumnIndex("AnswerD"));
- String correctAnswer = c.getString(c.getColumnIndex("CorrectAnswer"));
- String traloi1 = answerA.toUpperCase();
- String traloi2 = answerB.toUpperCase();
- String traloi3 = answerC.toUpperCase();
- String traloi4 = answerD.toUpperCase();
- String traloidung = correctAnswer.toUpperCase();
- Question question = new Question(Id, Image, traloi1.toUpperCase(), traloi2.toUpperCase(), traloi3.toUpperCase(), traloi4.toUpperCase(), traloidung.toUpperCase());
- listQuestion.add(question);
- }
- while (c.moveToNext());
- c.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- db.close();
- return listQuestion;
- }
- public void InsertScore(int Score){
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues contentValues = new ContentValues();
- contentValues.put("Score",Score);
- db.insert("Ranking",null,contentValues);
- }
- public List<Ranking> getHighScores(){
- List<Ranking> listHighScores = new ArrayList<>();
- SQLiteDatabase db = this.getWritableDatabase();
- Cursor c;
- try{
- c=db.rawQuery("SELECT * FROM High Scores ORDER BY Scores DESC;",null);
- if (c==null) return null;
- c.moveToNext();
- do{
- int Id = c.getInt(c.getColumnIndex("ID"));
- int Score = c.getInt(c.getColumnIndex("Score"));
- Ranking ranking = new Ranking(Id,Score);
- listHighScores.add(ranking);
- }
- while(c.moveToNext());
- c.close();
- }
- catch (Exception e){
- e.printStackTrace();
- }
- db.close();
- return listHighScores;
- }
- }
Add Comment
Please, Sign In to add comment