How to show useful error messages from a database error callback in Phonegap?

后端 未结 2 1929
臣服心动
臣服心动 2021-02-15 11:25

Using Phonegap you can set a function to be called back if the whole database transaction or the individual SQL statement errors. I\'d like to know how to get more informatio

2条回答
  •  暖寄归人
    2021-02-15 12:13

    Transaction object

    The only thing you can do with the transaction object is call its .executeSql() method, as far as I can ascertain. I cannot find any properties of this object.

    Error object

    The error object has a .code property which contains a number. You can either check the numerical value (see my original question above) or use something like:
    if (error.code == error.DATABASE_ERR) alert('nasty database error')

    The .message property is a string and may return something like this:

    • could not prepare statement (1 near "wibble": syntax error)
    • could not prepare statement (1 no such table: MyyTable)
    • could not prepare statement (1 table MyTable has no column named MyColunm)
    • could not execute statement (19 constraint failed)

    Other messages are possible! This is just the few I spotted when debugging in Chrome. I notice in Phonegap the messages are briefer: "no such table: MyyTable"

    There are two sets of success/error callbacks

    Also note that there is another database error callback on the initial call to .transaction(). Your function will only be returned an error object (no transaction object).

    The error's .code will be zero and the .message will be "the statement callback raised an exception or statement error callback did not return false".

    So remember to have your statement callbacks (function mentioned inside .executeSql such as my statement_error in the code example of my original question) return true or false depending on whether you want your transaction error callback (second function mentioned inside .transaction) to be hit. The 'success' callback you specified (third one inside .transaction) will be run if you return true (or don't return anything).

提交回复
热议问题