Titanium Community Questions & Answer Archive

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

Detecting Database Errors

How Would I go about catching database Errors like "Locked" in sqlite3.

— asked March 16th 2010 by Bryce Wilkinson
  • catch
  • database
  • desktop
  • errors
  • sqlite3
0 Comments

3 Answers

  • Accepted Answer

    you should be able to wrap your database calls in a try/catch block. in the catch, you can try to parse out the error number from the exception message - e.g.,

    try
    {
      // db calls
    }
    catch (e)
    {
       var message = e.message;  // parse this to get the error
    }
    

    here's a list of sqlite error codes:

    http://www.sqlite.org/c3ref/c_abort.html

    — answered March 16th 2010 by Nolan Wright
    permalink
    1 Comment
    • The database file will lock when a write action is being performed such as a insert, delete or update. You'll need to ensure that you close and/or end the transaction of any insert/delete/update queries to prevent a locking run-time error. It's also worth noting that currently a try-catch around the query execution will not catch any SQL error notice.

      Docs: "while the database supports concurrent read access, only one user may write to it at a time. This is because a filesystem lock is placed on the file during write operations. This is an important point to bear in mind with multi-threaded applications" – http://wiki.appcelerator.org/display/guides/Working+with+Local+Data

      — commented September 13th 2011 by Dan Boorn
  • [Old topic but may help a lot of people]
    Try catch is not working on databases. Check out the (tested and working!) answer of Simon Ferragne: http://developer.appcelerator.com/question/110201/how-to-trap-database-errors

    — answered September 5th 2011 by Robin D. (Perspecteev)
    permalink
    0 Comments
  • Robin,

    It works for me. Don't forget that you will need <script></script> markers around your code.

    — answered September 6th 2011 by Alan DuBoff
    permalink
    1 Comment
    • Robin,

      For clarity, I get: "ReferenceError: Can't find variable: dbPath

      — commented September 6th 2011 by Alan DuBoff
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.