Titanium Community Questions & Answer Archive

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

Updating Database

Hi,

I'm trying to update my database with the following line of code:

db.execute('UPDATE table SET val=? WHERE gID = ? VALUES(?,?)','true',gID)

I'm not really sure though what I'm doing and something with the above code is wrong. Does anyone know?

— asked October 29th 2010 by Connor Zwick
  • database
  • update
0 Comments

9 Answers

  • Likely this is the syntax you're looking for:

    db.execute('UPDATE table SET val=? WHERE gID=?','true',gID)
    

    That's assuming that "val" column type is string. If instead you want boolean, then you should switch to using integers (0 or 1) according to: http://www.sqlite.org/datatype3.html

    — answered October 29th 2010 by Todd Trann
    permalink
    0 Comments
  • That's what I had originally, and no it's a string. However, it still doesn't work. It just closes the app. Any ideas?

    — answered October 30th 2010 by Connor Zwick
    permalink
    0 Comments
  • Perhaps you can try an alternate syntax like

    db.execute('UPDATE table SET val=true WHERE gID='+gID)
    
    — answered October 30th 2010 by Pascal PLUCHON
    permalink
    0 Comments
  • Alright. I think I figured the source of my problem. I'm getting a SQLite error: 'database table is locked'. After a quick google search, it appears that I cannot update and read my database. What's the methodology around that for titanium?

    — answered October 30th 2010 by Connor Zwick
    permalink
    0 Comments
  • I've never encountered a 'table locked' error. My first guess would be to try to eliminate the obvious: mis-named table & database not open are two potential problems to check.

    As for "what is the methodology" part of your question, the db.execute technique you're using is the Titanium way of updating a database. Make sure you've done the var db = Titanium.Database.open('your_db'); first.

    Tim

    — answered October 30th 2010 by Tim Poulsen
    permalink
    0 Comments
  • So I should be able to update and insert in the same file? Good, because I saw that in the Kitchen Sink examples. And yes, I'm sure there are no typos. If I purposefully type something incorrect, then it gives me that error.

    So does anyone know what the source of my problem is? Thanks, this is becoming a real road block in my project.

    — answered October 30th 2010 by Connor Zwick
    permalink
    0 Comments
  • Looks like this is my issue:

    http://developer.appcelerator.com/question/49611/sqlite-locking-problem

    How do I close the INSERT statement?

    — answered October 30th 2010 by Connor Zwick
    permalink
    0 Comments
  • The implication in that other post is that a ResultSet was created and is still open on the table in question. In your code have you done a SELECT and stored the results in a ResultSet? If so, close that ResultSet before you attempt to do an UPDATE or INSERT.

    — answered October 30th 2010 by John McKnight
    permalink
    0 Comments
  • Do I still need to close an UPDATE execution, even if the results are not put into a variable?

    Thanks, updating no longer crashes my app. It just doesn't seem to actually be updating…

    — answered November 1st 2010 by Connor Zwick
    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.