how to include database file in DocumentsDirectory into device build

Hi,

I have a db stored in the DocumentsDirectory (inside project sandbox) and it works fine on simulator. I think when I build for the device the file is not included and I aways get error on device. If thats the case, how to include or preload the file into DocumentsDirectory?

I also found out that I could write to my db when I store it inside ResourcesDirectory (same folder as main.lua). This seem to be different from what the documents said, the ResourcesDirectory is read-only.

Hope to get some help on this. Thank you.

So Lin 

Hi So Lin,

Files in the local (Simulator) Documents sandbox will not be automatically copied to the app’s DocumentsDirectory when you build the app. If you want this database to be inside in the app’s documents directory, you’ll need to copy it from the ResourceDirectory to the DocumentsDirectory when the app first loads.

See this guide for more details on how to do this:

http://docs.coronalabs.com/guide/data/readWriteFiles/index.html#copying-files-to-subfolders

Best regards,

Brent

Hi Brent,

Can I delete the database file from ResourceDirectory after copying it to DocumentsDirectory? and Does the App need to run the code to check if file exist (not copy) or does exist (to copy) every time the App start? The ResourceDirectory document said:

For security reasons, this directory is read-only and enforced by the operating system, not by Corona.

If the directory is read-only, I’m afraid I can’t delete it (I haven’t try deleting it but I could add more record into the db so I assume I can delete too. If thats the case, the document should be corrected).

Thank you.

So Lin

Hi So Lin,

I would suggest that you create an empty database in the resources directory, then copy that over when the app starts up, and that copied version becomes the version that you add/delete records within. The original empty database would be essentially so small in size, it would be of no concern, so it would remain there. As for being able to edit a database in the resources directory, that shouldn’t be possible… are you absolutely sure it’s allowing you to edit that?

Brent

Hi Brent,

Thanks for the reply. I need to have preloaded data in the database (as it is my App’s content).

As for being able to edit data on ResourceDirectory, I have a search function that query one table and insert the result into another table and then display it in the App (I use another table as a temporary storage to keep the result because I need to go through the result (record) one by one later if the user need to). When I put the database in ResourceDirectory, the simulator display the result briefly and reset back to full content but things work fine on devices (no reset back to full content). So I assume I can modify my database when I store it on ResourceDirectory. Anyway I’ve already implemented the copy from Resource to Documents directory as you suggested and its working well :), thanks again for that.

So Lin

Hi So Lin,

Files in the local (Simulator) Documents sandbox will not be automatically copied to the app’s DocumentsDirectory when you build the app. If you want this database to be inside in the app’s documents directory, you’ll need to copy it from the ResourceDirectory to the DocumentsDirectory when the app first loads.

See this guide for more details on how to do this:

http://docs.coronalabs.com/guide/data/readWriteFiles/index.html#copying-files-to-subfolders

Best regards,

Brent

Hi Brent,

Can I delete the database file from ResourceDirectory after copying it to DocumentsDirectory? and Does the App need to run the code to check if file exist (not copy) or does exist (to copy) every time the App start? The ResourceDirectory document said:

For security reasons, this directory is read-only and enforced by the operating system, not by Corona.

If the directory is read-only, I’m afraid I can’t delete it (I haven’t try deleting it but I could add more record into the db so I assume I can delete too. If thats the case, the document should be corrected).

Thank you.

So Lin

Hi So Lin,

I would suggest that you create an empty database in the resources directory, then copy that over when the app starts up, and that copied version becomes the version that you add/delete records within. The original empty database would be essentially so small in size, it would be of no concern, so it would remain there. As for being able to edit a database in the resources directory, that shouldn’t be possible… are you absolutely sure it’s allowing you to edit that?

Brent

Hi Brent,

Thanks for the reply. I need to have preloaded data in the database (as it is my App’s content).

As for being able to edit data on ResourceDirectory, I have a search function that query one table and insert the result into another table and then display it in the App (I use another table as a temporary storage to keep the result because I need to go through the result (record) one by one later if the user need to). When I put the database in ResourceDirectory, the simulator display the result briefly and reset back to full content but things work fine on devices (no reset back to full content). So I assume I can modify my database when I store it on ResourceDirectory. Anyway I’ve already implemented the copy from Resource to Documents directory as you suggested and its working well :), thanks again for that.

So Lin