如何在Phonegap中显示来自数据库错误回调的有用的错误消息? [英] How to show useful error messages from a database error callback in Phonegap?

查看:349
本文介绍了如何在Phonegap中显示来自数据库错误回调的有用的错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Phonegap,您可以设置一个函数,如果整个数据库事务或单个SQL语句错误则回调。 我想知道如何获取有关错误的更多信息。



我有一个通用错误处理函数,以及许多可能触发它的不同SELECT或INSERT。 如何判断哪个故障?从错误消息中并不总是显而易见的。



我的代码到目前为止...

  function get_rows tx){
tx.executeSql(SELECT * FROM Blah,[],lovely_success,statement_error);
}
function add_row(tx){
tx.executeSql(INSERT INTO Blah(1,2,3),[],carry_on,statement_error);
}
function statement_error(tx,error){
alert(error.code +'/'+ error.message);
}



从各种示例中我看到错误回调将传递 transaction 对象和错误对象。我读到 .code 可以有以下值:




  • UNKNOWN_ERR = 0

  • DATABASE_ERR = 1

  • VERSION_ERR = 2

  • TOO_LARGE_ERR = 3

  • QUOTA_ERR = 4

  • SYNTAX_ERR = 5

  • CONSTRAINT_ERR = 6

  • TIMEOUT_ERR = 7



错误对象还有其他属性/方法吗?

什么是属性/方法事务对象在这一点?



我似乎找不到一个很好的在线参考。当然不在Phonegap网站上!

解决方案

交易对象



只有事情你可以做的事务对象调用其 .executeSql()方法,就我所知。我找不到此对象的任何属性。



错误对象



错误对象有 .code 属性包含一个数字。您可以检查数值(请参阅我上面的原始问题)或使用类似:

if(error.code == error.DATABASE_ERR)alert('nasty database error' )



.message 属性是一个字符串,可能返回如下:




  • 无法准备语句(靠近wibble:语法错误)

  • 无法准备语句(1表MyTable没有名为MyColunm的列)
  • $ b

    其他消息是可能的!这只是我在Chrome中调试时发现的几个。我注意到在Phonegap消息是更简单:没有这样的表:MyyTable



    有两组成功/错误回调



    还要注意,对 .transaction()的初始调用还有另一个数据库错误回调。你的函数只会返回一个错误对象(没有事务对象)。



    错误的 .code 将为零,并且 .message 将会语句回调引发一个异常或语句错误回调没有返回false



    所以记得要有你的语句回调(函数中提到的 .executeSql 如我的原始问题的代码示例中的statement_error)返回true或false取决于你是否想要你的事务错误回调 .transaction )。如果您返回true(或不返回任何内容),您指定的成功回调( .transaction 中的第三个)将运行。


    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 information about the error.

    I have one generic error-handling function, and lots of different SELECTs or INSERTs that may trigger it. How can I tell which one was at fault? It is not always obvious from the error message.

    My code so far is...

    function get_rows(tx) {
       tx.executeSql("SELECT * FROM Blah", [], lovely_success, statement_error);
    }
    function add_row(tx) {
       tx.executeSql("INSERT INTO Blah (1, 2, 3)", [], carry_on, statement_error);
    }
    function statement_error(tx, error) {
       alert(error.code + ' / ' + error.message);
    }
    

    From various examples I see the error callback will be passed a transaction object and an error object. I read that .code can have the following values:

    • UNKNOWN_ERR = 0
    • DATABASE_ERR = 1
    • VERSION_ERR = 2
    • TOO_LARGE_ERR = 3
    • QUOTA_ERR = 4
    • SYNTAX_ERR = 5
    • CONSTRAINT_ERR = 6
    • TIMEOUT_ERR = 7

    Are there any other properties/methods of the error object?
    What are the properties/methods of the transaction object at this point?

    I can't seem to find a good online reference for this. Certainly not on the Phonegap website!

    解决方案

    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).

    这篇关于如何在Phonegap中显示来自数据库错误回调的有用的错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆