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 :
- Get SQLite browser, manually insert each entry. I don't like this as it feels too "cowboy"
- 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 ins开发者_如何学Pythonerts.
Any ideas?
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?
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();
}
}
精彩评论