0

I am new for SQLite and here is the story;

I'm developing an apk which is a kind of quiz application. I want to store the questions and answers in SQLite database. ( I am also not sure if this is the best way to store data, in SQLite )

In onCreate() method I should create the db and insert all questions, answers etc. So, I'm not sure where to put the data that will be inserted to db before insert them to db tables.

I mean;

values.put("key", "What is abc");
** insert
values.put("key", "What is def");
** insert
values.put("key", "What is ghi");
** insert
values.put("key", "What is xyz");
** insert
...

where should I get those strings (What is bla bla) to insert to table ?

If this was a windows application, I would put them in a .txt file, read from there, and insert rows.

In android, I don't know.

Thanks for any suggestions.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Mtok
  • 1,600
  • 11
  • 36
  • 62

3 Answers3

2

I am also not sure if this is the best way to store data, in SQLite

There are worse choices. :-)

In onCreate() method i should create the db and insert all questions, answers etc.

I'd prepare the database ahead of time, package it with your app, and deploy it using SQLiteAssetHelper. This would be easier to write, easier to maintain, and faster to run.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • " I'd prepare the database ahead of time, package it with your app " is it possible ? :) – Mtok Mar 01 '14 at 22:46
  • @Mtok: Yes, it is possible. SQLite is used by hundreds of thousands of developers, and there are many clients for it (command-line, GUI, and browser-based). Set up your database on your development machine, then follow [the `SQLiteAssetHelper` instructions](https://github.com/jgilfelt/android-sqlite-asset-helper). – CommonsWare Mar 01 '14 at 22:58
0

You can use this to read a text file.

Once you have that in your SQLiteOpenHelper#onCreate() method you would add your default values.

Community
  • 1
  • 1
andrew
  • 166
  • 2
  • 5
0

One way to do this would be:

  1. Store the data as XML file in your res/raw folder.
  2. Create a Parser. (DOM would work for a small file. SAX would be better if the file size is large)
  3. Parse the data in onCreate and insert the values.

    For example: ` public void onCreate(SQLiteDatabase db) { ArrayList quizDataList;

        quizDataList = ParseQuizData.getQuizData(context);
        ContentValues values = new ContentValues();
        try {
            // Create Database
            db.execSQL(TABLE_CREATE);
            for (Iterator<QuizData> iterator = quizDataList.iterator(); iterator.hasNext();) {
                QuizData quizData = iterator.next();
                values.put(COLUMN1, quizData.getAttributeFirst());
                values.put(COLUMN2, quizData.getAttributeSecond());
    
                db.insertOrThrow(TABLE_NAME, null, values);
    
            }
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    

    `

Navjot Singh
  • 497
  • 2
  • 9
  • 1
    If you are going to go the XML route, put the XML in `res/xml/` and use the `XmlPullParser` you get back from a `Resources` object, as that will parse roughly 10 times faster. – CommonsWare Mar 01 '14 at 22:57