javascript 如何获取 Web Sql 错误的上下文?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16559832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the context of a Web Sql error?
提问by Matthieu
I start deploying an offline application on iPhones, but it's still under heavy developpment. I have a simple error handler for my query :
我开始在 iPhone 上部署离线应用程序,但它仍在大量开发中。我的查询有一个简单的错误处理程序:
db.transaction(tx) {
tx.executeSql("SELECT * FROM TABLE",[], successHandler, errorHandler);
});
function errorHandler(transaction, error) {
alert("Error : " + error.message);
}
When I test myself the application, and get an error, I manage to find what was the query generating the error. But when it's my users (distant users, of course), it's very difficult, as the error messages are not specific.
当我测试自己的应用程序并收到错误时,我设法找到了生成错误的查询是什么。但是当是我的用户(当然是远程用户)时,这是非常困难的,因为错误消息并不具体。
Is there a way to add context info to my error messages, for example the sql query, or a comment parameter ?
有没有办法将上下文信息添加到我的错误消息中,例如 sql 查询或注释参数?
回答by Myrne Stol
You could use a pattern like this:
你可以使用这样的模式:
db.transaction(tx) {
doQuery(tx, "SELECT * FROM TABLE",[],theSuccessHandler)
});
function doQuery(tx, query, values, successHandler) {
tx.executeSql(query, values, successHandler, errorHandler);
function errorHandler(transaction, error) {
alert("Error : " + error.message + " in " + query);
}
}
回答by Matthieu
I ehanced Myrne answer to add query parameters and a free context string :
我增强了 Myrne 的答案以添加查询参数和一个免费的上下文字符串:
function doQuery(tx, query, values, successHandler, context)
{
tx.executeSql(query, values, successHandler, errorHandler);
function errorHandler(transaction, error)
{
var text_context = context != undefined && context != "" ? "(" + context + ") " : "";
alert("Error "+text_context+": " + error.message + " in " + query + " (params : "+values.join(", ")+")");
}
}
This will return this kind of error :
这将返回这种错误:
Error (function update_commande) : could not prepare statement (1 no such column: field3) in UPDATE table SET field2 = ?, field3 = ? WHERE field1 = ? (params : 1.63, 1449, 606)
错误(函数 update_commande):无法在 UPDATE 表中准备语句(1 没有这样的列:field3) SET field2 = ?, field3 = ? WHERE 字段 1 = ? (参数:1.63、1449、606)