Cordova&SQLite-所有行项目返回为'未定义'

Cordova & SQLite - All row items returning as 'undefined'

本文关键字:返回 未定义 项目 amp SQLite- Cordova      更新时间:2023-09-26

我正在Netbeans IDE中使用Cordova框架构建一个Android应用程序。我已经建立了一个只有几个表的小型数据库,并且能够将数据插入到表中,但是,我发现很难检索数据。

每次我查询数据库时,所有项目都返回为未定义项。数据插入很好,因为每次我添加新行并调用以下命令时,行数都会增加:

var len = results.rows.length;
alert(len);

这是我的代码:

function readRecord(){
    var db = window.openDatabase("dbResults", "1.0", "Results DB", 1000000);
     db.transaction(getRecord, errorCB);
}
function getRecord(tx){
    try
    {
        tx.executeSql('SELECT [time], bglevel FROM RECORDS', [], recordSuccess, errorCB);
    }
    catch(err)
    {
        alert(err.message);
    }
}
function recordSuccess(tx, results) {
    var len = results.rows.length;
    alert(len);
    var res = '';
    for (var i=0; i<len; i++){
        try
        {
            alert("Row = " + i.toString() + " Time =  " + results.rows.item(i).time);
            alert("Row = " + i.toString() + " BG =  " + results.rows.item(i).bglevel);
        }
        catch(err)
        {
            alert(err.message);
        }
        res = res + '(' +  results.rows.item(i).time + '|' + results.rows.item(i).bglevel + '),';
        alert(res);
    }     
}

当我运行这个时,最后的警报只有以下文本:

(undefined,undefined),
(undefined,undefined),
(undefined,undefined),
(undefined,undefined),

如有任何帮助,将不胜感激

您需要通过行,而不是项目,比如:

alert("Row="+i.toString()+"Time="+results.rows[i].item.Time);

在index.js中创建一个变量var db;这个db更像是一个全局变量,其值在整个应用程序中都保持不变。

现在在onDeviceReady()中初始化您的数据库。

  var db;
    var app={
    initialize: function() {
            this.bindEvents();
        },
        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
            document.addEventListener("pause", function(){ console.log("--- paused ---"); isAppPaused = true;}, false);
        },
        onDeviceReady: function() {
              if(window.sqlitePlugin !== undefined) {
                console.log('opening sqlite DB ');
                db = window.sqlitePlugin.openDatabase("ECM_MOBILE");
            } else {
                console.log('opening Web SQL DB ');
                db = window.openDatabase("ECM_MOBILE", "1.0", "Cordova Demo", 200000);
            }   
       this.doSomething(db);         
        },
    doSomething:function(db){
    }
  };
app.initialize();

这里有一个执行任何sql语句的示例,这里的数据库是您的数据库,sql是作为字符串和回调函数传递的sql语句。

read: function(database, sql, callback){
    this.callback = callback;
    this.sql = sql;
    database.transaction(
      function(tx){
          app.doRead(tx);
      }, 
        function(error){
          app.onError(error);
        }
    );  
  },
 onError: function(error){
    console.log("error call back : " + JSON.stringify(error));
    console.log(error);
   },
 onResult: function(tx, result){
    console.log("result call back : " + result);
    this.callback(null, result);
  },
doRead: function(tx){
    console.log("execute sql : " + this.sql);
    tx.executeSql(this.sql , [], 
        function(tx, result){
          app.onResult(tx, result);
        }, 
        function(error){
          app.onError(error);
        }
    );    
  }

希望它能有所帮助,你需要相应地集成它来检查解决方案,也尝试CCD_ 1;关于返回的结果和CCD_ 2。

希望能有所帮助。