How can I embed an SQLite database into an application?

前端 未结 4 951
情话喂你
情话喂你 2020-12-04 11:36

I think I have some basic understanding problem so maybe someone\'s able to help :-)

I\'m developing an Android application using Eclipse and this application will m

4条回答
  •  清歌不尽
    2020-12-04 11:58

    I've done this in the past by storing a JSON file in the application in the res/raw resources and then loading the file on first load. From there, you can use the bulk insert mode to batch-import entries. See my database loader for Units as an example of this technique. One benefit of the res/raw style is that you can use the Android resource selecting system to localize the data to different regions, so you could have a different database (or part thereof) for different languages or regions.

    You could also put a raw SQL dump in a similar file and load that on first load. You can get the file from the resources by using openRawResource(int). I'd recommend this instead of storing a pre-made sqlite database file to increase compatibility between versions of sqlite, as well as make maintaining the database easier (from an app-development lifecycle POV).

    When loading things in bulk, make sure to use transactions, as that'll help speed things up and make the loading more reliable.

提交回复
热议问题