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 crash - can't close

I have a list that I am populating from a database. I am following the example found in the persistence app. This is the code:

var db = Titanium.Database.open('MyTable');
db.execute('CREATE TABLE IF NOT EXISTS labels (id INTEGER PRIMARY KEY, label TEXT)');


//Code that populates database if it doesn't exist

var dbData = db.execute('SELECT * FROM labels');

for(i=0;i<dbData.rowCount;i++){
    labels.push(dbData.fieldByName('label'));
    Ti.API.info(dbData.fieldByName('label'));
    dbData.next();
}
dbData.close();

Everything works fine until I quit the application when I get the following stack trace saying that the db cannot be closed because implementations have leaked prepared statements.

If I add db.close(); to the code the same error is thrown.

I tried looking at different questions but I can't figure this out.

Any help is appreciated.

— asked July 26th 2010 by Abraham Vivas
  • close
  • crash
  • database
  • error
  • iphone
0 Comments

1 Answer

  • I would close the db after populating the data, and define db again to open the database before select, then close it again.

    — answered July 26th 2010 by Daniel Lim
    permalink
    7 Comments
    • If I do that, I get the same error 'leaked prepared statements'. I don't understand

      — commented July 26th 2010 by Abraham Vivas
    • BTW I am using Titanium Mobile SDK 1.3.3

      — commented July 26th 2010 by Abraham Vivas
    • I don't know what you got in populating data parts, something like this still gives you problem?

      var db = Titanium.Database.open('MyTable');
      db.execute('CREATE TABLE IF NOT EXISTS labels (id INTEGER PRIMARY KEY, label TEXT)'); 
      //Code that populates database if it doesn't exist
      db.close();
      
      var db = Titanium.Database.open('MyTable');
      var dbData = db.execute('SELECT * FROM labels');
      
      for(i=0;i<dbData.rowCount;i++){
          labels.push(dbData.fieldByName('label'));
          Ti.API.info(dbData.fieldByName('label'));
          dbData.next();
      }
      dbData.close();
      db.close();
      

      — commented July 26th 2010 by Daniel Lim
    • It did….I siwtched to 1.3.2 and it seems to work so it is a possible bug in what 1.4 is going to be. It is working now with 1.3.2

      — commented July 26th 2010 by Abraham Vivas
    • Interesting, i use lots of insert, open and delete db on my iphone app, it's running with 1.3.3 and i have not seen error like that. How many data you're inserting?

      — commented July 26th 2010 by Daniel Lim
    • Its less than 50 in the table…..

      — commented July 26th 2010 by Abraham Vivas
    • Up above I'm seeing:

      dbData.close();
      db.close();
      

      Are you supposed to close the database and the result set? The persistence app only closes the result set (dbData.close();) and there's no "close" option in the database API docs.

      — commented August 17th 2010 by Clifton Labrum
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.