Phonegap Sqlite处理SQL时出错:6

时间:2015-07-13 14:38:52

标签: sqlite cordova

我是法国人很抱歉我的英语不好,我需要一些帮助,我正在尝试将我的网络服务器上的数据库复制到我的phonegap应用程序中以制作本地数据库,所以我就是这样:

function dlDatabase(){
        var db = window.openDatabase("Databases", "1.0", "Cordova Demo", 200000);
        db.transaction(createAllTables, errorCB, dlCountry);
    }


    function createAllTables(tx){
        tx.executeSql('CREATE TABLE IF NOT EXISTS Country (id integer primary key, name text, nbQuestion integer)');
        tx.executeSql('CREATE TABLE IF NOT EXISTS Question (id integer primary key, text text, CountryId integer)');
        tx.executeSql('CREATE TABLE IF NOT EXISTS Answer (id integer primary key, QuestionId integer, text text, isGood boolean)');
    }

    function insertCountry(tx, id, name, number){
        tx.executeSql("INSERT INTO Country(id, name, nbQuestion) VALUES (?,?,?)", [id, name, number]);
    }

    function dlCountry() {
        alert("dlCountry");
        $.ajax({
            type: "POST",
            url: url,     
            dataType: "json",
            data : {
                actionname : 'dlCountry'
            },
            success: function(data) {
                arrayCountry = data.arrayCountry;
                var db = window.openDatabase("Databases", "1.0", "Cordova Demo", 200000);
                db.transaction(function(tx){
                    for (var i = 0; i < arrayCountry.length; i++) {
                        alert(arrayCountry[i].id+" "+arrayCountry[i].name+" "+arrayCountry[i].number);
                        insertCountry(tx, arrayCountry[i].id, arrayCountry[i].name, arrayCountry[i].number);
                    };
                }, errorCB, dlQuestion);
            },
            error: function(data) {
                alert("error !");
            }
        });
    }

我登录时调用dlDatabase函数,这是有效的,但是,在插入所有国家/地区之后(警报之后),我有一个错误代码6:

function errorCB(err) {
    alert("Error processing SQL: "+err.code);
}

我是SQLite的初学者,错误代码6是什么,以及如何解决它? 谢谢您的帮助! :)

1 个答案:

答案 0 :(得分:0)

首先,将alert("Error processing SQL: "+err.code);更改为alert("Error processing SQL: "+err.message);

就像在cordova / phonegap文档中描述的那样...

  

操作a时发生错误时抛出SQLError对象   数据库中。

此对象有两个属性:

  • 代码(您描述的常量之一)
  • 消息:错误说明

这个问题的详细答案可以在这里找到 - &gt; How to show useful error messages from a database error callback in Phonegap?

就像你在该链接中看到的那样,错误代码6意味着

  

CONSTRAINT_ERR = 6

有关约束错误的信息可在此处找到 - &gt; http://www.w3schools.com/sql/sql_constraints.asp我现在要检查你的代码。至于我能解答您的具体问题,我即可进行编辑。到目前为止,为了您的信息。