How to access the database using WebSQL?

可紊 提交于 2019-12-13 02:19:30

问题


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 "

code:

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.

Thanks in Advance.


回答1:


Try something like this:

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 );
}

Also, why only 2MB database?

-- edit --

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.



来源:https://stackoverflow.com/questions/19021013/how-to-access-the-database-using-websql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!