Ti.Database.install works in mysterious ways

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

Hello ppl,

Now I am trying to work with databases. It's still iPad 4.2 and Ti SDK 1.6.0. I am using Database.install and my application just quits on start. At first I couldn't see any error messages but then I found where the log file was. So it sais database file can not be found. I looked up the path and noticed that install actually copied the database file but the path is wrong. The path goes only to the folder name where the database file is. The file name itself is missing in the path. Like so:

path: "/Users/mindvitaminsab/Library/Application Support/iPhone Simulator/4.2/Applications/C9905AF4-821C-4CEE-A657-B9B38B5DA0E4/Library/Application Support/database/abc.sqlite.sql"

file: "/Users/mindvitaminsab/Library/Application Support/iPhone Simulator/4.2/Applications/C9905AF4-821C-4CEE-A657-B9B38B5DA0E4/Library/Application Support/database/abc.sqlite.sql/abc.sqlite"

And here's the line of code:

var db = Ti.Database.install('db', 'abc.sqlite');
So the question is: am I doing something wrong?

Thanks in advance

P.S. Another interesting detail is I change the copied file name to abc.sqlite.sql and remove the folder with the same name. Then I can use

var db = Ti.Database.open('abc.sqlite');
Now I have access to my database.

1 Answer

Accepted Answer

Eugen,

I think you have the arguments reversed in your original line of code:

var db = Ti.Database.install('db', 'abc.sqlite');

This tells it to look to see if you have previously opened a database called abc.sqlite and if so, just open it again and ignore anything in the Resources folder. If you have not previously opened abc.sqlite, then it looks in the Resources folder for a file called 'db' (without extension) and copies it to the application's database folder and calls it 'abc.sqlite'

If 'abc.sqlite' is a file in your Resources folder, then you could do:

var db = Ti.Database.install('abc.sqlite', 'db');

Which would attempt to (re)open a database called "db" but when there is not yet, look in Resources for a file called 'abc.sqlite' for the initial copy. More typical usage would be:

var db = Ti.Database.install('abc.sqlite', 'abc');

But the name (db vs abc) doesn't really matter if you always use the same .install() values to open the database. However if you later close the database and want to reopen in your code you would either need to use the same .install() values or use .open() with the name in the second argument of the first .install() that was executed.

— answered 2 years ago by Doug Handy
answer permalink
2 Comments
  • Thanks Doug,

    I think I start to understand where I went wrong. I have a "abc.sqlite" file in my resource folder but in subfolder called "db". If I use var db = Ti.Database.install('abc.sqlite', 'abc'); is it going to find the file even if it is in subfolder? I'll try to test it later. Right now I am fighting with dynamic tableView and eventListner on cell level. :)

    — commented 2 years ago by Eugen Stolin

  • Eugen,

    The path will need to be relative to where the current *.js file is located. If the code is in your app.js (or other file directly in the Resources folder), then you would need:

    var db = Ti.Database.install('db/abc.sqlite', 'abc');

    But if the current *.js was in a different subfolder try:

    var db = Ti.Database.install('../db/abc.sqlite', 'abc');

    where the .. would back up to the parent (Resources) folder, then drill down to the db subfolder.

    — commented 2 years ago by Doug Handy

Your Answer

Think you can help? Login to answer this question!