Android OrmLite pre-populate database

隐身守侯 提交于 2019-12-22 04:45:08

问题


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?


回答1:


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/




回答2:


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();
}


来源:https://stackoverflow.com/questions/5054974/android-ormlite-pre-populate-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!