Reputation: 16174
I'm using titanium and testing against an android emulator - but any advice relevant to iOs is also welcome!
i am trying to use titanium with a database. I'm using the firefox sql lite plugin to make my db - so i make it, and then i go
database > export database > to a location in my titanium app project that is under "Resources" folder
ok, neat.
Then i have this code:
var db = Ti.Database.install('/db/wibbler.sql','wibbler');
function getLanguages(){
var sql = 'select * from language order by name desc';
var results = [];
var resultSet = db.execute(sql);
while (resultSet.isValidRow()){
results.push({
name: resultSet.fieldByName('name'),
id: resultSet.fieldByName('id'),
desctiption: resultSet.fieldByName('description')
});
reultSet.next();
}
resultSet.close();
return results;
}
As you can see, the location of the file is
Resources/db
and the db file is called "wibbler.sql"
The problem is, when i run my app, it complains that the sql i'm using refers to a table that doesn't exist - to wit:
uncaught error: no such table
What is the deal?
Upvotes: 0
Views: 1449
Reputation: 16174
Ok, so the answer is a bit of a mix - something from what Adam suggested, and some other stuff that i didn't realise
First up: my file was just an exported sqllite database. That's not right - the file should be the actual database file (so, you know, the "location" of the database that firefox makes should reside somewhere in the Resources folder, and titanium should point to that).
What cleared this up was this tutorial: http://blogs.usask.ca/the_bolt/archive/2010/12/titanium_tutorial_database.html
As noted in the tutorial, it is out of date - but all i needed to know was that I was using the wrong file in my titanium code.
Next, as Adam mentions, android will keep on using the old db - you can clean it out each time, but that is a hassle. Try this instead:
var db = Ti.Database.open('wibbler');
db.remove();
db = Ti.Database.install('/db/wibbler.sqlite','wibbler');
that finds the database, kills it, and the re-installs it. Also note that i'm now pointing to the sqlite file, not the sql file.
Hooray!
Upvotes: 0
Reputation: 1432
When you first run var db = Ti.Database.install('/db/wibbler.sql','wibbler');
, the database file at Resources/db/wibbler.sql
will be installed either in Private Documents (iOS) or internal or external storage (Android). This new copy of the db is where your var db will be pointing. Docs.
When you run the app a second time, since the database is already installed, it will simply open that copy. So, if you ran the app, then added a table to your database file under References
, that new table won't be reflected in the copy of the db.
To get around this, you can do one of the following:
'wibbler2'
Upvotes: 1