处理SQL时出错:无法准备语句(15列的11个值)

时间:2017-02-28 07:03:38

标签: javascript json sqlite

我正在尝试开发一个phonegap应用程序。我正在尝试从URL获取JSON数据并将其存储到SQLite数据库中。当我这样做时,我收到了一个错误:

  

处理SQL时出错:无法准备语句(15的值为1 1   列)

我的代码如下。

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height" />
    <script src="js/jquery.js"></script>
    <script src="js/jquery.mobile-1.3.2.min.js"></script>

        <script type="text/javascript">
            var db,did;
            function loaddb()
            {
                db = window.openDatabase("Watersenz", "1.0", "Cordova Demo", 2*1024*1024);
                db.transaction(createDB, errorCB, successCB);
            }
            function createDB(tx) {
                tx.executeSql('DROP TABLE IF EXISTS USERIDDETAILS');
                tx.executeSql('CREATE TABLE IF NOT EXISTS USERIDDETAILS (deviceid TEXT NOT NULL)');
            }
            function errorCB(err) {
                //alert("Error processing SQL: "+err.code);
                console.log("Error processing SQL: "+err.message);
            }
            function successCB() {
                console.log("success");
            }
            function insertDB(tx) {
                did = $("[name='deviceid']").val();
                var sql='INSERT INTO USERIDDETAILS(deviceid)VALUES(?)';
                tx.executeSql(sql,[did],sucessQueryDB,errorCB);
                console.log(did);
            }   
            function sucessQueryDB(tx){
                alert("selected");
                tx.executeSql('SELECT * FROM USERIDDETAILS',[],renderList,errorCB); 
            }
            function renderList(tx,results){ 
            alert("rendering");
                var htmlstring='';
                var len=results.rows.length;
                alert("rendered called"+ len);
                for(var i=0;i<len;i++){
                    htmlstring += '<li>'+ results.rows.item(i).deviceid+'</li>';
                }   
                $('#resultList').html(htmlstring);
                $('#resultList').listview('refresh');
            }

            function onClick(){
                db.transaction(insertDB,errorCB);
                $.ajax({
                    url : "https://api.thingspeak.com/channels.json?api_key=xxxxxxxxx",
                    dataType:"json",
                    cache: false,
                    error:function (xhr, ajaxOptions, thrownError){
                    debugger;
                            alert(xhr.statusText);
                            alert(thrownError.code);
                        },
                    success : function(json) {
                    console.log("Entering getIdList.success()");
                    console.log(json);
                    alert(json);
                    if (json.length == 0) {
                        window.alert("The returned output array length is ZERO.");
                    } else {
                        var obj, des , md ,apiKeys;
                        //i=channels object length
                        for (var i=0; i<json.length; i++) {
                            obj = json[i];
                            //console.log(obj);
                            if (obj == null || obj == "") {
                                window.alert("\n The "+(i+1)+"th object is NULL/BLANK.");
                            } else {
                                if (obj.id == did) {

                                if (obj.name == null || obj.name == "") {
                                        window.alert("\n The name portion of "+(i+1)+"th object is NULL/BLANK.");
                                    } else {
                                        console.log("\n The deails of "+(i+1)+"th Object are :  \nDname : " + obj.name);
                                        alert("\n The deails of "+(i+1)+"th Object are :  \nDname : " + obj.name);
                                        dnamedb();                               
                                        dnameForm();

                                        var name = obj.name;
                                        console.log(name);
                                        function dnamedb(){
                                            db = window.openDatabase("Watersenz", "1.0", "Cordova Demo", 2*1024*1024);
                                            db.transaction(creatednDB, errorCB, successCB);
                                            alert("dname");
                                        }
                                        function creatednDB(tx) {
                                            tx.executeSql('DROP TABLE IF EXISTS DEVICENAME');
                                            tx.executeSql('CREATE TABLE IF NOT EXISTS DEVICENAME(Dname TEXT NOT NULL)');
                                            alert("done");
                                        }

                                        function insertdnDB(tx){
                                            var sql = "INSERT INTO DEVICENAME(Dname) VALUES ('"+name+"')";
                                            tx.executeSql(sql);
                                            console.log(name);
                                        }

                                        function dnameForm(){
                                            db.transaction(insertdnDB,errorCB);
                                            return false;
                                        }
                                    }

                                if (obj.description == null || obj.description == "") {
                                    window.alert("\n The description portion of "+(i+1)+"th object is NULL/BLANK.");
                                } else {
                                    des = JSON.parse("{" + obj.description + "}");
                                    console.log("\n The deails of "+(i+1)+"th Object are :  \nName : " + des.n + "\nEmailid : " + des.e + "\nMobile : " + des.m + "\nAddress-1 : " + des.a1 + "\nAddress-2 : " + des.a2+ "\nSumpheight : " + des.sh+ "\nSumplength : " + des.sl+ "\nSumpwidth : " + des.sw+ "\nSumpcapacity : " + des.sc+"\nTanklength : " + des.tl+ "\nTankwidth : " + des.tw+ "\nTankheight : " + des.th+ "\nTankdensity : " + des.td+ "\nTankcapacity : " + des.tc+ "\nMotorpower : " + des.mp);
                                    loaduddb();
                                    userdetForm();

                                    function loaduddb()
                                    {
                                        db = window.openDatabase("Watersenz", "1.0", "Cordova Demo", 2*1024*1024);
                                        db.transaction(createudDB, errorCB, successCB);
                                        console.log("kkkk");
                                    }
                                    function createudDB(tx) {
                                        tx.executeSql('DROP TABLE IF EXISTS USERDETAILS');
                                        tx.executeSql('CREATE TABLE IF NOT EXISTS USERDETAILS(Name TEXT NOT NULL,Emailid TEXT NOT NULL,Mobile INTEGER NOT NULL,Address1 TEXT NOT NULL,Address2 TEXT NOT NULL,Sumpheight INTEGER NOT NULL,Sumplength INTEGER NOT NULL,Sumpwidth INTEGER NOT NULL,Sumpcapacity INTEGER NOT NULL,Tanklength INTEGER NOT NULL,Tankwidth INTEGER NOT NULL,Tankheight INTEGER NOT NULL,Tankdensity INTEGER NOT NULL,Tankcapacity INTEGER NOT NULL,Motorpower INTEGER NOT NULL)');
                                        console.log("done ud");
                                    }

                                    function insertudDB(tx){
                                        var sql = "INSERT INTO USERDETAILS(Name,Emailid,Mobile,Address1,Address2,Sumpheight,Sumplength,Sumpwidth,Sumpcapacity,Tanklength,Tankwidth,Tankheight,Tankdensity,Tankcapacity,Motorpower) VALUES ('"+ des.n + '","' + des.e + '","' + des.m + '","' + des.a1 + '","' + des.a2+ '","' + des.sh+ '", "' + des.sl+ '","' + des.sw+ '","' + des.sc+ '","' + des.tl+ '","' + des.tw+ '","' + des.th+ '","' + des.td+ '","' + des.tc+ '","' + des.mp + "')";
                                        tx.executeSql(sql);
                                        console.log(des.n,des.e,des.m,des.a1,des.a2,des.sh,des.sl,des.sw,des.sc,des.tl,des.tw,des.th,des.td,des.tc,des.mp);

                                    }
                                    function userdetForm(){
                                        db.transaction(insertudDB,errorCB);
                                        return false;

                                    }

                                }

                                if (obj.metadata == null || obj.metadata == "") {
                                    window.alert("\n The metadata portion of "+(i+1)+"th object is NULL/BLANK.");
                                } else {
                                    md = JSON.parse("{" + obj.metadata + "}");
                                    console.log("\n The deails of "+(i+1)+"th Object are :  \nTankno : " + md.tn + "\nTankano : " + md.ta + "\nTankeid : " + md.te + "\nCleanerno : " + md.cn + "\nCleanerano : " + md.ca + "\nCleanereid : " + md.ce+"\nPno : " + md.pn+ "\nPano : " + md.pa+ "\nPeid : " + md.pe+ "\nEno : " + md.en +"\nEano : " + md.ea +"\nEeid : " + md.ee +"\nMno : " + md.mn +"\nMano : " + md.ma +"\nMeid : " + md.me +"\nMm : " + md.mm +"\nMmdl : " + md.mmdl +"\nMmfd : " + md.mmfd +"\nMprd : " + md.mprd);
                                    loadmddb();
                                    metadataForm();
                                    function loadmddb()
                                    {
                                        db = window.openDatabase("Watersenz", "1.0", "Cordova Demo", 2*1024*1024);
                                        db.transaction(createmdDB, errorCB, successCB);
                                        console.log("md loaded");
                                    }
                                    function createmdDB(tx) {
                                        tx.executeSql('DROP TABLE IF EXISTS METADATA');
                                        tx.executeSql('CREATE TABLE IF NOT EXISTS METADATA(Tankno INTEGER NOT NULL,Tankano INTEGER NOT NULL,Tankeid TEXT NOT NULL,Cleanerno INTEGER NOT NULL,Cleanerano INTEGER NOT NULL,Cleanereid TEXT NOT NULL,Pno INTEGER NOT NULL,Pano INTEGER NOT NULL,Peid TEXT NOT NULL,Eno INTEGER NOT NULL,Eano INTEGER NOT NULL,Eeid TEXT NOT NULL,Mno INTEGER NOT NULL,Mano INTEGER NOT NULL,Meid TEXT NOT NULL,Mm INTEGER NOT NULL,Mmdl INTEGER NOT NULL,Mmfd INTEGER NOT NULL,Mprd INTEGER NOT NULL)');
                                        console.log(" md done");
                                    }

                                    function insertmdDB(tx){
                                    var sql = "INSERT INTO METADATA(Tankno,Tankano,Tankeid,Cleanerno,Cleanerano,Cleanereid,Pno,Pano,Peid,Eno,Eano,Eeid,Mno,Mano,Meid,Mm,Mmdl,Mmfd,Mprd) VALUES ('" +md.tn+ '","' +md.ta+ '","' +md.te+ '","' +md.cn+ '","' +md.ca+ '" ,"' +md.ce+ '","' +md.pn+ '","' +md.pa+ '","' +md.pe+ '","' +md.en+ '","' +md.ea+ '","' +md.ee+ '","' +md.mn+ '","' +md.ma+ '","' +md.me+ '","' +md.mm+ '","' +md.mmdl+ '","' +md.mmfd+ '","' +md.mprd+"')";
                                        tx.executeSql(sql);
                                        console.log(md.tn,md.ta,md.te,md.cn,md.ca,md.ce,md.pn,md.pa,md.pe,md.en,md.ea,md.ee,md.mn,md.ma,md.me, md.mm,md.mmdl,md.mmfd,md.mprd);
                                    }

                                    function metadataForm(){
                                        db.transaction(insertmdDB,errorCB);
                                        return false;

                                    }
                                }   

                                if (obj.api_keys == null || obj.api_keys.length == 0) {
                                    window.alert("\n The api_keys portion of "+(i+1)+"th object is NULL/BLANK.");
                                } else {
                                    apiKeys = obj.api_keys;
                                    //j=channel apikey length
                                    for (var j = 1; j < apiKeys.length; j++) {
                                    console.log("\n The deails of "+(i+1)+"th Object are :  \nApikey : " + apiKeys[j].api_key + "\nWriteflag:" + apiKeys[j].write_flag);
                                    loadakdb();
                                    apikeyForm();

                                    var apikey = apiKeys[j].api_key;
                                    console.log(apikey);
                                    var writeflag = apiKeys[j].write_flag;
                                    console.log(writeflag);
                                    function loadakdb()
                                    {
                                        db = window.openDatabase("Watersenz", "1.0", "Cordova Demo", 2*1024*1024);
                                        db.transaction(createakDB, errorCB, successCB);
                                        console.log("ak loaded");
                                    }
                                    function createakDB(tx) {
                                        tx.executeSql('DROP TABLE IF EXISTS APIKEYS');
                                        tx.executeSql('CREATE TABLE IF NOT EXISTS APIKEYS(Apikey TEXT NOT NULL,Writeflag TEXT NOT NULL)');
                                        console.log("ak done");
                                    }

                                    function insertakDB(tx){
                                        var sql = "INSERT INTO APIKEYS(Apikey,Writeflag) VALUES ('" +apikey+ '","' +writeflag+ "')";
                                        tx.executeSql(sql);
                                        console.log(apikey,writeflag);
                                    }

                                    function apikeyForm(){
                                        db.transaction(insertakDB,errorCB);
                                        return false;

                                    }
                                    }
                                    }
                                $.ajax({
                                    url : "https://api.thingspeak.com/channels/"+did+"/feeds.json?api_key="+apikey,
                                      dataType: "json",
                                      cache: false,
                                      error: function(xhr, ajaxOptions, thrownError) {
                                        debugger;
                                        alert(xhr.statusText);
                                        alert(thrownError);
                                      },
                                      success: function(json1) {
                                      console.log(json1);

                                        json1.feeds.forEach(function(feed,i){
                                          console.log("\n The details of " + i + "th Object are :  \nCreated_at: " + feed.created_at
                                          + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2+"\nField3:" + feed.field3);      
                                            feedsdb();
                                            feedsForm();

                                        function feedsdb()
                                            {
                                                db = window.openDatabase("Watersenz", "1.0", "Cordova Demo", 2*1024*1024);
                                                db.transaction(createcfDB, errorCB, successCB);
                                                console.log("feeds load");
                                            }
                                            function createcfDB(tx) {
                                                tx.executeSql('DROP TABLE IF EXISTS FEEDS');
                                                tx.executeSql('CREATE TABLE IF NOT EXISTS FEEDS(Created_at TEXT NOT NULL,Entry_id, TEXT NOT NULL,Field1 INTEGER NOT NULL,Field2 INTEGER NOT NULL,Field3 INTEGER NOT NULL )');
                                                //console.log("feeds done");
                                            }

                                            function insertcfDB(tx){ 
                                                var sql = "INSERT INTO FEEDS(Created_at,Entry_id,Field1,Field2,Field3) VALUES ('" +feed.created_at+ '","' +feed.entry_id+ '","' +feed.field1+ '","' +feed.field2+ '","' +feed.field3+ "')";
                                                tx.executeSql(sql);
                                                console.log(feed.created_at,feed.entry_id,feed.field1,feed.field2,feed.field3);
                                                db.transaction(sucessfeedsDB,errorCB);
                                            }
                                            function sucessfeedsDB(tx){
                                                alert("selected");
                                                tx.executeSql('SELECT * FROM FEEDS',[],renderList1,errorCB);    
                                            }
                                            function renderList1(tx,results){ 
                                                alert("rendering");
                                                var htmlstring='';
                                                var len=results.rows.length;
                                                alert("rendered called"+ len);
                                                for(var i=0;i<len;i++){
                                                    htmlstring += '<li>'+ results.rows.item(i).feed.created_at+'<br>'+results.rows.item(i).feed.entry_id+'<br>'+results.rows.item(i).feed.field1+'<br>'+results.rows.item(i).feed.field2+'<br>'+results.rows.item(i).feed.field3+'</li>';
                                                }   
                                                $('#resultFeed').html(htmlstring);
                                                $('#resultFeed').listview('refresh');
                                            }
                                            function feedsForm(){
                                                db.transaction(insertcfDB,errorCB);
                                                return false;
                                            }

                                        }); 

                                      }
                                    });
                                }                   
                            }           
                        }
                    }   
                }
            });                         
            window.open("new4.html");
            return false;
        }
    </script>
</head>   
<body>
    <div id="header">
        <h3> Login </h3>
        <script type="text/javascript">
            $(document).ready(function() {
            loaddb();
            }); 
        </script>
    </div>
    <div id="section">
        <form id="myForm">
             <br><input type="text" class="one" name="deviceid" id="deviceid" placeholder="DeviceId"><br>
             <input type="button" class="button" value="SUBMIT" onclick="return onClick()">
        </form>

    </div>

在上面的代码中,我从描述部分得到错误,即函数loaduddb()。它正在插入值并抛出上述错误,同样的错误继续metadata(function loadmddb())apikeys(function loadakdb())feeds(function feedsdb())部分也是如此。任何人都可以帮我解决这个问题.Thankyou

0 个答案:

没有答案