Database installation

You must Login before you can answer or comment on any questions.

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.

— answered 2 years ago by Doug Handy
answer permalink
4 Comments
  • I don't see install method in the desktop documentation. I haven't tried it yet, not sure if it will work.

    — commented 2 years ago by Alex Casanova

  • Hmmm, did not realize that. I never use Desktop. Looking at it now, it seems for a sqlite db you should use openFile and not open. I don't know if the resources folder is read-only in desktop or not.

    You may be able to just use openFile on that location. If that will not accept updates, roll your own precheck for the database in a writable folder. If not there, copy it from resources. Then use openFile.

    — commented 2 years ago by Doug Handy

  • Yea I think I'll try that. Sounds like the best bet.

    — commented 2 years ago by Alex Casanova

  • Show 1 more comment

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

— answered 2 years ago by Udo Hempel
answer permalink
1 Comment
  • Sorry, there is a slip of the pen ;)

    var db = Titanium.Database.openFile(dbpath); not var db = dbTitanium.Database.openFile(dbpath);

    — commented 2 years ago by Udo Hempel

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!