使用JSON数组填充phonegap sqlite数据库

时间:2014-03-28 15:40:53

标签: javascript json cordova

我试图使用JSON数组填充phonegap sqlite数据库,获取JSON的Web服务正在运行并且数据加载得很好但是当我尝试将其插入到我的表中时它会一直给我一个undefined sql错误。

这是我的代码:

function populateDB(tx) {
    //Setup database table
    tx.executeSql('DROP TABLE IF EXISTS Profiles');
    tx.executeSql('CREATE TABLE IF NOT EXISTS Profiles (id unique, name, email, phone)');
    //Get data
    var values;
    var base_url = "https://www.hoomz.nl/staging/index.php/api/";
    $.getJSON(base_url + 'profiles', function(result) {
        $.each(result, function(i, item) {
            console.log(item.name);
            console.log('(' + item.id + ', "' + item.id + '", "' + item.id + '", "' + item.id + '"),');
            values = values + '(' + item.id + ', "' + item.id + '", "' + item.id + '", "' + item.id + '"),';
        });
        console.log(values);
        tx.executeSql('INSERT INTO Profiles (id, name, email, phone) VALUES' + values + ';');
    });
    console.log(values);
    tx.executeSql('INSERT INTO Profiles (id, name, email, phone) VALUES' + values + ';');
}

也会出现此错误: Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

3 个答案:

答案 0 :(得分:5)

在这种情况下抛出的错误是,当SQL引擎尝试插入值时,对象不再存在。您应该将JSON结果存储在全局变量中,然后执行SQL操作。请尝试下面的代码并查看结果。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
   var db = null;
   var resultJSON;
   function insertIntoDB() {
       db.transaction(function (tx){
       tx.executeSql('DROP TABLE IF EXISTS Profiles');
       tx.executeSql('CREATE TABLE IF NOT EXISTS Profiles (id , name, email, phone)');
       var recursiveFunction = function(index) {
        if (index < resultJSON.length) {
            tx.executeSql('INSERT INTO Profiles (id, name, email, phone) VALUES (?, ?, ?, ?)',
               [resultJSON[index].id,resultJSON[index].name,resultJSON[index].email,resultJSON[index].phone], function (){index++; recursiveFunction(index)}, errorCB);
         }
      }
      recursiveFunction(0);
    });
   }

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

   function onDeviceReady() {
     db = window.openDatabase("test", "1.0", "Test DB", 1000000);
     var base_url = "https://www.hoomz.nl/staging/index.php/api/";
     $.getJSON(base_url + 'profiles', function(result) {
      resultJSON = result;        
      insertIntoDB();
     });
    }
    document.addEventListener("deviceready", onDeviceReady, false);
  </script>

答案 1 :(得分:0)

我想我看到了你的问题。您正在循环每个结果并附加到值。在循环之后,你做插入。但是如果你有2个或更多结果,那么values变量看起来像

( stuff here)(stuff here)(stuff here)

您可能希望在每个插入内部插入。

答案 2 :(得分:0)

你做得有点不对劲。你正在进行迭代&#34;对于每个循环&#34;并且您将值插入字符串变量&#34; Value&#34;在每次迭代中导致错误,因为插入包含4列但是&#34;值&#34;包含第n个值,请尝试以下代码:

 function populateDB(tx) {
    //Setup database table
    tx.executeSql('DROP TABLE IF EXISTS Profiles');
    tx.executeSql('CREATE TABLE IF NOT EXISTS Profiles (id unique, name, email, phone)');

    //Get data
    var values;
    var base_url = "https://www.hoomz.nl/staging/index.php/api/";
    $.getJSON(base_url + 'profiles', function (result) {
        $.each(result, function (i, item) {
            console.log(item.name);
            console.log('(' + item.id + ', "' + item.id + '", "' + item.id + '", "' + item.id + '"),');
            tx.executeSql('INSERT INTO Profiles (id, name, email, phone) VALUES(' + item.id + ', "' + item.id + '", "' + item.id + '", "' + item.id + '")');
        });
    });
}