Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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!

— asked June 28th 2010 by Kelly Redd
  • crash
  • database
  • install
  • open
0 Comments

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.

    — answered June 28th 2010 by Damien Elmes
    permalink
    0 Comments
  • 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();
    });
    
    — answered June 29th 2010 by Kelly Redd
    permalink
    1 Comment
    • Try removing the Db.open line from the save button since you should be able to use the previously defined db object.

      — commented June 29th 2010 by Sj Singh
  • Can you post the db file somewhere, I'll test it on my end

    — answered June 29th 2010 by Sj Singh
    permalink
    0 Comments
  • 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

    — answered June 29th 2010 by Kelly Redd
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.