2

I have an android app, which is basically an exam simulator, it will come preloaded with questions and the user gets a simple multiple choice interface.

My question is, what is the best way to preload, or at least manage the task of preloading data into a SQL database?

Two options I can see right off the bat :

  1. Get SQLite browser, manually insert each entry. I don't like this as it feels too "cowboy"
  2. Create some utility class that gets run once and inserts a bunch of data, I don't like this either as I don't think that hardcoding a load of inserts in a class file is good practice.

What is the best option (open to alternative suggestions) for this problem? Can I have a SQL insert script that gets loaded on startup?

The data will be static, if it needs to change I'll publish a new version of the app. I expect there to be 200~ questions, so I won't have masses of inserts.

Any ideas?

Jimmy
  • 16,123
  • 39
  • 133
  • 213
  • The questions come in some form, no? -- be it JSON or XML or CSV, etc. Just take that and insert the data into SQLite as appropriate. Alternatively, SQLite databases can be distributed (as they have a neutral binary format) and attached so the data might not even to be in the "primary" database. Then the creation step is only in putting the data into this distributed database -- using whatever tooling that is desired (e.g. while still on a PC). –  Jul 22 '11 at 20:05

2 Answers2

2

This website explains it well:

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

Basically, you put the db in your assets folder, then the first time you run the app the db is copied to the right place, and you can use it.

I think this is a duplicate of this question: How can I embed an SQLite database into an application?

Community
  • 1
  • 1
aha
  • 462
  • 4
  • 11
  • just a quick question - if the phone is rooted, then your app's private DB files become transferable (to other rooted device perhaps). Is this still the case for non-rooted android devices ? – kellogs Aug 14 '12 at 17:17
  • I have not found a way to copy an app's private DB files from that device, specifically on non-rooted devices. – aha Aug 14 '12 at 22:31
0

I did feed the database the cowboy way only for the arrays. Just helped myself for the queries with this

BufferedWriter bufferedWriter = null;

            String filename = "queries.txt";
            File file = new File(filename);
            String a = "\"";
            try {

                file.createNewFile();
                bufferedWriter = new BufferedWriter(new FileWriter(filename));

                int b=1;
              for(int i=0;i<restLat.length;i++)
              {



                String temp="INSERT INTO table VALUES (                  );";


                bufferedWriter.write(temp);
                bufferedWriter.newLine();

                }
            } catch (FileNotFoundException ex) {
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {

                try {
                    if (bufferedWriter != null) {
                        bufferedWriter.flush();
                        bufferedWriter.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148