2

I have been gathering information in this site and others about the best way to include a pre-populated sqlite database in Android. I just would like to confirm that I understood the reasons of why something that should be trivial… it is not.

So could someone please tell me if my following conclusions are correct or wrong?:

  • the main reason many people suggest copying a pre-populated database file from the assets folder to "/data/data/YOUR_PACKAGE/databases/" is because there is no way to access a database file in the assets folder or doing that would be overly complex (?). (Could someone clarify which of these two answers is the right one ?)
  • another important reason database files in the assets folder must be copied somewhere else is because files in that location cannot be updated. Then even if a database in the assets folder could be open, this would be useful only if such database does not have to be modified. UPDATE: I launched a new thread focussing only on this issue: Opening a read only database directly in the assets folder.
  • files in the assets folder can be only 1 Mb size (unless they have certain file extensions such as mp3). Note that this restriction is not relevant if: your database is smaller that 1 Mb, or you do not mind dividing your database in 1 Mb chunks and putting them together at runtime, or you do not mind distributing a database file with a mp3 extension.
  • If the database file is copied from the assets folder to "/data/data/YOUR_PACKAGE/databases/", there is no way to delete the original database file at the assets folder to avoid having a duplicated file. This is also because files in the assets folder cannot be modified.

Making the puzzle a bit more complex: I found in the comments to the accepted answer of this question: Ship an application with a database that copying the database from the assets folder to another location in fact does not work on some devices running 2.3+. Is that accurate ? If that is true, then the best alternative would be to download the database file from the web at first run ?

Thanks for any clarification.

Community
  • 1
  • 1
Sergio
  • 8,532
  • 11
  • 52
  • 94
  • 1
    read this one mate http://www.mail-archive.com/android-developers@googlegroups.com/msg28194.html there're size restrictions on the assets/raw folders too. – Sergey Benner Jan 18 '12 at 01:50
  • 1
    everything in the assets folder is static and cannot be modified, so you have to copy. – Bill Gary Jan 18 '12 at 02:40
  • assets,xml drawables and other such resources are part of apk so they cannot be programatically updated.And root permission is reqd to view data is "data/data" – Its not blank Jan 18 '12 at 03:26
  • You can download the db from a server at first run. – Alex Jan 18 '12 at 11:44

1 Answers1

1

You're essentially confusing apks with actual folders on your device.

Think of an apk as an install package - not unlike the msi of the Windows world. The whole goal of this install package is to securely authenticate and deliver code and resources to your device. In a naive implementation, you would then unpack said code to a read-only location, the resources somewhere read-writeable and be on your merry way.

To save space, Android is a bit smarter - the code and resources never leave the signed archive, so you always know it's the ones you put in and you don't waste space by storing the code twice. There's some real magic going on in the class loader that also allows it to unzip classes on the fly but that's besides the point.

So, essentially, everything in this compressed install package is read-only (by virtue of also being signed). It's your job to be the "installer" and move whatever resources you need to a read-writeable location. Of course, you can't touch the apk once it's in place since that would allow for malware and defeat the whole purpose of the signing.

Hope this clears the confusion.

Delyan
  • 8,881
  • 4
  • 37
  • 42
  • +1 Thanks @Delyan, your answer clarified why everything in the apk is read only. But assuming that my database is read only: is there a way to open it from its original location in the apk (the assets folder) without having to copy it first somewhere else ? – Sergio Jan 18 '12 at 13:28
  • I've not tried it but `file:///android_asset/db.sqlite` might work (note the three slashes). If it doesn't, though, it would take some extensive hackery to get around that limitation. – Delyan Jan 18 '12 at 13:44
  • I have just opened a separate thread for that part of my question: ( http://stackoverflow.com/questions/8914673/opening-read-only-databases-in-the-assets-folder ). From what I asked originally, still it is not clear for me the last point: if the method of copying the database file to another location is still valid since apparently it has been reported it fails in certain devices running 2.3+ (as mentioned in the link of my question). Do you know something about that @Delyan ?. In any case you answered most of my doubts so I am accepting your answer. – Sergio Jan 18 '12 at 18:03
  • No idea where those reports come from. It's entirely possible it's a bug in some vendor's implementation. Some of the vendors have a history of abusing SQLite.. – Delyan Jan 18 '12 at 18:40