2

We have several tables in our app and each one is per populated via a RAW sql file when the db is created, ie app installed first time. ie using

executeSqlScript( dao, sqlFile )

We have 1 sql file for each table. This is straight forward at the moment but as number of tables grows so too will the number of sql files. Is there any better way to populate the database? Would storing the static data in an XML file and deserializing this into Domain objects and persisting these via OrmLite be a better option or is there some other trick I am missing?

MayoMan
  • 4,757
  • 10
  • 53
  • 85
  • http://stackoverflow.com/questions/9109438/how-to-use-an-existing-database-with-an-android-application/9109728#9109728 – Yaqub Ahmad Nov 08 '12 at 02:38

2 Answers2

0

there certainly is. if you have one table for each file, you could do well to merge these files into one sql file or even a db file directly with a database browser

mango
  • 5,577
  • 4
  • 29
  • 41
0

I was also deciding about how to access the SqLite database in one Android app. Finally I have end up with OrmLite. Basically because I don't like hardcoding of SQL statements to Java code. I was searching for something similar to Hibernate but "lite" enough for Android.

OrmLite not only creates your tables according to your domain objects, so you don't need to write SQL create scripts. But it is also quite easy to populate the SqLite database just by creating Java objects and saving them to DB.

Only time I had to write SQL scripts was when I was upgrading the database using the OrmLiteSqliteOpenHelper.onUpgrade() method. In this case there is need to write the ALTER TABLE statements. Otherwise you can use pure Java to manipulate SqLite DB. And it is up to you if you create 1 Java file per table. Or you populate multiple tables in a single Java file. The good thing about this is that you write Java code which is verified by Java compiler. Which is not true if you hardcode SQL statements to Strings.

If you have lot of initialization data, and you can get the data in XML files easily. Then deserializing XML to domain objects and storing them to DB using OrmLite might be a solution as well.

petrsyn
  • 5,054
  • 3
  • 45
  • 48
  • I wrote a library time ago... it focuses on performance since I sometimes feel OrmLite is too slow. Although, since it is still in beta I have only used on projects I lead: https://github.com/casidiablo/persistence – Cristian Nov 08 '12 at 00:41
  • 1
    Looks like we might go with ORMlite then for initializing the DB. We use it anyways for our persistence. Perfoemance has not been a problem so far so hopefully will be ok. @Cristian: Must take a look at that library of yours – MayoMan Nov 08 '12 at 10:02