database.open() crashes and where does insert go?
Hello all,
I have an 'in-app' database that opens fine with db = Titanium.Database.install('db/theFeed.db', 'theFeedDb');
but crashes when i try to use database.open('db/theFeed.db') - which is what i think i should be using since the app already exists.
also, when i try to insert a record i can see that it is successful by using:
Titanium.API.info('JUST INSERTED, rowsAffected = ' + db.rowsAffected);
Titanium.API.info('JUST INSERTED, lastInsertRowId = ' + db.lastInsertRowId);
but if i close/reopen the app i get nothing. i've read somewhere that Ti copies the database or something to that effect. so it seems like i'm opening and reading from one db and inserting into another. how do i only use one database???
thx!
4 Answers
-
Install will only install once, so you can call it on subsequent invocations of the app to get back the same DB.
If you want to use open, you should use "theFeedDb", not the path to the file in your resources folder.
-
Thanks for clearing that up for me, Damien. Unfortunately it didn't stop the crashing. This is my first attempt to use a database and it's so undocumented that i'm sure i've got something else out of whack. Below are the steps i'm taking to insert into the database. does this look wrong to anyone?? sry, big chunk of code!
Step 1: Install database and then query database (working)
db = Titanium.Database.install('db/theFeed.db', 'theFeedDb'); var feeds = db.execute('SELECT * FROM feed'); data = []; while (feeds.isValidRow()) { data.push({title:feeds.field(1), live_url:feeds.field(2), archive_url:feeds.field(3)}); feeds.next(); } // close database feeds.close();
Step 2: Open new window and pass database (working)
newfeed_btn.addEventListener('click', function(e){ var newfeed = Ti.UI.createWindow({ url: 'files/newfeed.js'); newfeed.db = db; newfeed.open(); });
Step 3: Open and insert data (hardcoded values for testing purposes) - (NOT working)
save_feed.addEventListener('click', function(e){ db.open('theFeedDb'); db.execute('INSERT INTO feed (name, liveUrl, archiveUrl) VALUES(?,?,?)','test site','live url','archived url'); Titanium.API.info('JUST INSERTED, rowsAffected = ' + db.rowsAffected); Titanium.API.info('JUST INSERTED, lastInsertRowId = ' + db.lastInsertRowId); newfeed.close(); });
-
Can you post the db file somewhere, I'll test it on my end
-
Thanks Sj, removing the db.open() line seems to have stopped the crashing. but i'm still not seeing the new records. still seems to be some disconnect b/n the database i open(install) on launch and the database i'm writing to. i'm still printing to the console and seeing a new row inserted but if i close/relaunch the app it still doesn't return the new record(s).
Here is my db file. much thx.
http://cl.ly/92fb6002a90fa0f0ad9f