如何使用WebSQL访问数据库? [英] How to access the database using WebSQL?

查看:138
本文介绍了如何使用WebSQL访问数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序,并且我有一个现有的数据库.我正在尝试使用WebSQL从数据库中获取数据,但收到错误消息为"ExceptionReferenceError:未定义tx"

I'm develop a web app and I have an existing database.I'm trying to getting the data from database using WebSQL but getting error as "ExceptionReferenceError: tx is not defined "

代码:

var databaseSync = null;

try 
{
   databaseSync = openDatabase("database", "1.0", "Database", 10 * 1024 * 1024);
   databaseSync.transaction(function(tx){
      tx.executeSql('SELECT distinct(Subject) FROM Data', [], function (tx, cb_results){
       var len = cb_results.rows.length;
      var results = [];
  for (i = 0; i < len; i++)
  {
    var row = cb_results.rows.item(i);
      $("#list").append('<li><a href="#" id="">'+ row.Subject +'</a></li>');
          $("#list").listview('refresh');
  }    
      }, function(){console.log("Transaction success");}, function(tx, error){console.log("Error" + error.message);});

   });
}
catch (e) {
    // TODO: handle exception
    console.log("Exception" +e );
}

将结果附加到列表视图中,但未获取.

Appending the results in the listview but not getting.

预先感谢.

推荐答案

尝试如下操作:

var databaseSync = null;
try 
{
   databaseSync = openDatabase("database", "1.0", "Database", 2 * 1024 * 1024);
   databaseSync.transaction(function(tx){
      tx.executeSql('SELECT distinct(Subject) FROM Data', [], function (tx, cb_results){
      var len = cb_results.rows.length;
      var results = [];
  for (i = 0; i < len; i++)
  {
    var row = cb_results.rows.item(i);
    results[i] = row;
   }        
      console.dir(results);
      }, function(){console.log("Transaction success");}, function(tx, error){console.log("Error" + error.message);});

   });
}
catch (e) {
    // TODO: handle exception
    console.log("Exception" +e );
}

还有,为什么只有2MB的数据库?

Also, why only 2MB database?

-编辑-

好的,这是一个在实时项目中使用的函数示例:

OK, here is an example of a function, as used in a live project:

function return_icd10_vwxy_headers(callback)
{
    var results = [];
    session_storage_database_handle.transaction(function (tx) {
        tx.executeSql("SELECT  mainid, key, description FROM qicd10vwxy WHERE parent = '0';", [], function (tx, query_result) 
        {
            var len = query_result.rows.length;
            for (i = 0; i < len; i++){
                var row = query_result.rows.item(i);
                results[i] = {
                                mainid: row["mainid"],
                                key: row["key"],
                                description: row["description"]
                            };
            }
            callback(results)
        });
    });
}

也许您可以看到自己错过的东西.

Maybe you can see something you missed.

这篇关于如何使用WebSQL访问数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆