2

I've worked with the sql lite database from Android before, but I always executed queries one by one. Now I need to execute a lot of queries to initialize a database of cities.

So I used to do

sql = "THE QUERY";
myDatabase.execSql(sql);

Now I want to execute a file like the following: http://joeylemmens.be/downloads/postcodes_en_gemeenten.sql

What's the best way to do this?

I could store the whole file in a string and then do the above again, but there must be something better.

Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
Stephan Celis
  • 2,492
  • 5
  • 23
  • 38

3 Answers3

4

If your aim is to initialize the SQLite database with some default values then you should have version of the database having these default values, you can provide your application along with that default database version.

The other approach to read from a file & execute the sqls is also fine & i can not think of a better solution.

EDIT:

How to use already created database? see this.

Community
  • 1
  • 1
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
2

Store the SQL string in assets as a text file, read to a string using AssetManager, execute as usual.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
2

If you have multiple statements in one file, Android's API won't support it.

Take a look here how you can split the file into separate statements and execute them: https://github.com/greenrobot/greenDAO/blob/master/DaoCore/src/de/greenrobot/dao/DbUtils.java

PS.: Yes, you should download the file and store it info a string before.

Markus Junginger
  • 6,950
  • 31
  • 52