Android studio conection with SQL table DB browser

192 views Asked by At

Straight to point:

I am making a Quizz app in Android studio. I would like to get the question (and answers) that will fill the app from SQL table that i made in DB browser for SQLite.

My question is how do i connect those two (without rettyping all those question in android studio).

I have in somethin like a structure {ID, qusetion, answer} and I only point ID number i whant to fetch from the table and it fills my array in android studio.

I hope this make sense :)

Regards

2

There are 2 answers

0
Boldijar Paul On

You could put every question and answer in a file, line by line, things separated by a specific character.

ex:

What's the color of the sky?;Blue;Red;Yello;Black
Who is Harry potter?;A magician;A girl;A burrito;A car

And read the file, and insert everything them in the database on the first time you open the app.

2
Michael Dodd On

Assuming you're able to export from DB Browser to a .sql file (including CREATE TABLE statements as you'll effectively be creating a new database from scratch), you can use this exported file to recreate your database in your app.

By overriding the SQLiteOpenHelper.onCreate() method, you could do something like this:

@Override
public void onCreate(SQLiteDatabase db) {
    // Open the raw SQL file
    InputStream inputStream = context.getResources().openRawResource(R.raw.mydb);
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

    // Read the file
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      sb.append(line);
    }
    reader.close();

    // Pump the statements into the database
    String queries = sb.toString()
    for (String query : queries.split(";")) {
        db.execSQL(query);
    }
}

Replacing mydb with whatever the name of your sql file is in /res/raw