0

I have this inquiry about the Android database [in particular of SQLite version 3]. As we all know, in-order to create a database we need create the tables and table records on the class or file [convert then into database if necessary].

But this process would take time upon the execution of the table creation and default table records if any. Is there a way to omit this execution time? Or rather is there a way to inject the *.db directly to the *.apk to avoid the insertion of default table records?

Thanks in advance

David B
  • 3,269
  • 12
  • 48
  • 80

3 Answers3

0

I've looked into this before, and the answer seems to be a resounding no.

Phil Lello
  • 8,377
  • 2
  • 25
  • 34
  • But how about .txt or .dat? Isn't placing them on the /res folder would lead to injecting on the *.apk upon build? – David B Oct 23 '11 at 05:07
  • Yes, but it would need copying out of the apk before you could use it. The performance gains would be minimal, and bloat the apk. – Phil Lello Oct 23 '11 at 05:11
0

I'd think you could put the .db file in the /assets folder and then read it at runtime with:

getResources().getAssets().open("mybundleddb.db")

You'd then need to write the content out to your data directory before attempting to access it with database code.

goto10
  • 4,370
  • 1
  • 22
  • 33
  • As noted in another comment, this will not necessarily save much execution time. – goto10 Oct 23 '11 at 05:13
  • But if we have condition if the *.db isn't located in the application data then we copy the *.db [with default table records inserted]; would it save some time if we're dealing with a 600 records overall? – David B Oct 23 '11 at 05:17
  • It might save some time but I couldn't say for sure. I'd probably just time it both ways. There are a number of tricks to getting the best performance out of SQLite when doing a lot of inserts, but that's probably beyond the scope of this question. Copying the db would be the simpler route, I think. – goto10 Oct 23 '11 at 05:21
  • I see, can you supply me with the code on how to copy the *.db from the /res to the app data and how validate if the *.db is already in the app data? Thanks, I'll accept then your answer. – David B Oct 23 '11 at 05:26
  • I don't have time to write that all for you right now, but I can give some tips: the line in my answer returns an input stream. There are lots of examples for reading from an input stream and writing to a file. You can get the path for your database on the device from ContextWrapper.getDatabasePath("yourdbname.db") – goto10 Oct 23 '11 at 05:41
0

Not exactly sure whether this answers your question, anyways you can look into this which explains how to take your own SQLite database file from the "assets" folder and copy into the system database path of your application so the SQLiteDatabase API can open and access it normally. Also have a look at the answers to this question. Ship an application with a database

Community
  • 1
  • 1
Primal Pappachan
  • 25,857
  • 22
  • 67
  • 84