Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android OrmLite pre-populate database

Is it possible with OrmLite to create a sql script file to easily populate the database with data? I did some searching and couldn't come up with anything easy. I know I can create some objects with data, I'm just looking for a cleaner method.

I'm thinking create a script file, open a a reader at load, and process each file as raw SQL the executeRaw() method. Any thoughts?

like image 985
joey_g216 Avatar asked Feb 20 '11 03:02

joey_g216


2 Answers

Just wanted to post my solution for anyone who might need it

try {
    tableDAO.updateRaw("DELETE FROM table");
    InputStream is = getResources().openRawResource(R.raw.populate_db);
    DataInputStream in = new DataInputStream(is);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    while ((strLine = br.readLine()) != null) {
        tableDAO.updateRaw(strLine);
    }
    in.close();
} catch (Exception e) {
    e.printStackTrace();
}
like image 71
joey_g216 Avatar answered Sep 23 '22 08:09

joey_g216


Good one Joe. I think your idea of the executeRaw() is close but use updateRaw() instead. Update handles INSERT, DELETE, and UPDATE statements.

http://ormlite.com/docs/raw-update

You should call TableUtils to create your schema first of course:

http://ormlite.com/docs/tableUtils

Hope this helps. You may want to use the mailing list for questions in the future:

http://groups.google.com/group/ormlite-user/

like image 39
Gray Avatar answered Sep 22 '22 08:09

Gray