0

Reposting this question as the previous one has been removed. Upon further research I know that in order to store images in sqlite database is to use a "blob" or something like that. I found this thread

which I don't even know if this the one close to what I need because There are tons of different ways of putting it. And I just need someone to atleast help me fit it in my codes. I wanna try and understand it with my best but I am in a rush here and I must finish this fast.

class TriviaQuizHelper extends SQLiteOpenHelper {
    private ImageView imageView;
    private Context context;

    private static final String DB_NAME = "easy11111.db";

    //If you want to add more questions or wanna update table values
    //or any kind of modification in db just increment version no.
    private static final int DB_VERSION = 3;
    //Table name
    private static final String TABLE_NAME = "easy";
    //Id of question
    private static final String UID = "_UID";
    //Question
    private static final String QUESTION = "QUESTION";
    //Option A
    private static final String OPTA = "OPTA";
    //Option B
    private static final String OPTB = "OPTB";
    //Option C
    private static final String OPTC = "OPTC";
    //Option D
    private static final String OPTD = "OPTD";
    //Answer
    private static final String ANSWER = "ANSWER";
    //So basically we are now creating table with first column-id , sec column-question , third column -option A, fourth column -option B , Fifth column -option C , sixth column -option D , seventh column - answer(i.e ans of  question)
    private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ( " + UID + " INTEGER PRIMARY KEY AUTOINCREMENT , " + QUESTION + " VARCHAR(255), " + OPTA + " VARCHAR(255), " + OPTB + " VARCHAR(255), " + OPTC + " VARCHAR(255), " + OPTD + " VARCHAR(255), " + ANSWER + " VARCHAR(255));";
    //Drop table query
    private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;

    TriviaQuizHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //OnCreate is called only once
        sqLiteDatabase.execSQL(CREATE_TABLE);
    }



    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        //OnUpgrade is called when ever we upgrade or increment our database version no
        sqLiteDatabase.execSQL(DROP_TABLE);
        onCreate(sqLiteDatabase);

    }




    void allQuestion() {
        ArrayList<TriviaQuestion> arraylist = new ArrayList<>();




        arraylist.add(new TriviaQuestion("1+1",
                "2", "3", "5", "8", "2"));

        arraylist.add(new TriviaQuestion("2+2",
                "2", "3", "4", "5", "4"));
        arraylist.add(new TriviaQuestion("3+3",
                "2", "3", "5", "6", "6"));
        arraylist.add(new TriviaQuestion("4+4",
                "8", "7", "6", "5", "8"));
        arraylist.add(new TriviaQuestion("5+5",
                "9", "8", "10", "11", "10"));

        this.addAllQuestions(arraylist);

    }


    private void addAllQuestions(ArrayList<TriviaQuestion> allQuestions) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();
        try {
            ContentValues values = new ContentValues();
            for (TriviaQuestion question : allQuestions) {
                values.put(QUESTION, question.getQuestion());
                values.put(OPTA, question.getOptA());
                values.put(OPTB, question.getOptB());
                values.put(OPTC, question.getOptC());
                values.put(OPTD, question.getOptD());
                values.put(ANSWER, question.getAnswer());
                db.insert(TABLE_NAME, null, values);
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
            db.close();
        }
    }


    List<TriviaQuestion> getAllOfTheQuestions() {

        List<TriviaQuestion> questionsList = new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();
        String coloumn[] = {UID, QUESTION, OPTA, OPTB, OPTC, OPTD, ANSWER};
        Cursor cursor = db.query(TABLE_NAME, coloumn, null, null, null, null, null);


        while (cursor.moveToNext()) {
            TriviaQuestion question = new TriviaQuestion();
            question.setId(cursor.getInt(0));
            question.setQuestion(cursor.getString(1));
            question.setOptA(cursor.getString(2));
            question.setOptB(cursor.getString(3));
            question.setOptC(cursor.getString(4));
            question.setOptD(cursor.getString(5));
            question.setAnswer(cursor.getString(6));
            questionsList.add(question);
        }

        db.setTransactionSuccessful();
        db.endTransaction();
        cursor.close();
        db.close();
        return questionsList;
    }
}


spolopos
  • 11
  • 5
  • 1
    why don't you save the image inside the device, and use sqlite to save the path to it? – Rudy_TM Feb 20 '20 at 16:53
  • That's the other thing that I read online. I don't know how to do that and what's the difference between that and doing that blob thing. this is my other reference: https://www.youtube.com/watch?v=HR-3fm1Pcxg – spolopos Feb 20 '20 at 17:23
  • I would recommend the same as Rudy_TM, its a better pratice to store path/filename in the database. Read the path/filename, save it. And when you want to show the image somewhere, you know where to find the file... – Esocoder Feb 20 '20 at 19:09
  • 3
    Does this answer your question? [How to incorporate images into SQLite quiz?](https://stackoverflow.com/questions/56184410/how-to-incorporate-images-into-sqlite-quiz) – Esocoder Feb 20 '20 at 19:13

0 Answers0