Cannot open database file
I'm trying to connect to an SQLite database, but I can't get it working. I tried to calling openFile on a nonexisting file hoping it will create one. I then create a db file with Firefox's SQLite Manager and then tried to open with openFile, but i get the "Cannot open database file".
Here's the code: http://www.pastie.org/1011928
2 Answers
-
Accepted Answer
var db = Titanium.Database.openFile(Titanium.API.application.dataPath, "baza.sqlite");
replace above code with the following:
var db = Titanium.Database.openFile(Titanium.API.application.dataPath, "\baza.sqlite");
check weather the path is correct or not by doing the following:
var adp=Titanium.API.Application.getDataPath();
alert(adp);// here u should get base path;
var dbpath = adp+"\data.db";
alert(dbpath);// here u should get total path by concatenating data.db;
var db = Titanium.Database.openFile(dbpath); -
If you have a database that exists / ships with your app then use Database.install() like:
//Open/install the database var db = Titanium.Database.install('../existingdb.db', 'myDatabase');
You can call that multiple times as install will only occur once.
Creating a new database use Database.open :
var db = Titanium.Database.open('todos'); db.execute('CREATE TABLE IF NOT EXISTS todos (id INTEGER PRIMARY KEY, todo TEXT)');
Check the persistance example app as it has working examples.