Hello, I'd like to distribute my desktop app with a prefilled sqlite file. I've seen that developers seem to be putting the database in the application's resource directory. However the docs suggest not to write to this directory. Would it be better to keep a bare bones copy of the database in resources directory, then move the database to the user's document directory? What are the best practices on this? If a user of the app updates will they lose the data in the database file in the resource folder? Thank you
4 Answers
Each device has a default location for where it stores a database. When you open your database for the first time, it will be placed in the device specific database location.
If you want to include a default set of tables and records with your app, put it in the resources folder then use Ti.Database.install instead of Ti.Database.open when accessing the file.
The install method takes two arguments, the complete filename (and path if not in the same folder as the current *.js) and the name to call the database (the same name you would use on an open method).
The install method first checks if a database by that name already exists. If so, it simply opens it as if you used the open command. If it does not already exist, it copies the file you specify to the database folder and then opens it. Thus new users get your default set of tables (and rows, if any) but existing users just reopen the existing database.
Note that you do NOT also need to use the open method. Use install instead of open. If it needs to copy the file first it will, then it will open it for you.
Regarding where the location is for desktop applications, I can't say because I only use the mobile version. But if nothing else, run your project once then do a computer search for the root name of your database.
I used the php-function copy () for an issue like yours.
Have a look: http://www.php.net/manual/en/function.copy.php
It´s called at appstart, you can call it after every db update or, maybe with a synchronize-button, manually.
Maybe you can use
Titanium.Filesystem.File.copy
If you have a prefilled database in recources-folder, you don´t need to install it. Use
var db = dbTitanium.Database.openFile(dbpath); var execute = db.execute(SQLstatement);For the paths use:
Titanium.Filesystem (have a look in APIreference)
Hope, it helps
Indeed , Titanium.Database.install is not a desktop sdk method.
I use the approach below,
app.db.install=function(){ var adp = Titanium.API.Application.getDataPath(); try{ var installedDb=Titanium.Filesystem.getFile(adp,'dbFile.sqlite'); if(!installedDb.exists()){ var dbFile=Titanium.Filesystem.getFile(Titanium.Filesystem.getResourcesDirectory(),'dbFile.sqlite'); dbFile.copy(adp); } } catch(e){ alert(e); } }; app.db.install(); try{ var db = Titanium.Database.openFile(Titanium.Filesystem.getFile(Titanium.API.Application.getDataPath(),'dbFile.sqlite')); } catch(e){ alert(e); }You can change the function to be reusable, but is I use it only once I keep it like this...
Your Answer
Think you can help? Login to answer this question!