0

我正在尝试使用 webSQL 从数据库中检索用于 Web 应用程序的数据,但我无法从数据库中获取数据。我对此很陌生。我试过这样

var DB_NAME    = "database";
var DB_VERSION = "";
var DB_TITLE   = "";
var DB_BYTES   = 50 * 1024 * 1024;
var db = openDatabase(DB_NAME, DB_VERSION, DB_TITLE, DB_BYTES);

//Retrieve Rows from Table
db.transaction(
    function(tx) {
        tx.executeSql("SELECT * FROM Data;",
            [],
            function (tx, results) {
  var len = results.rows.length, i;
  for (i = 0; i < len; i++) {
    alert(results.rows.item(i).text);
  }
});

});

提前致谢。

4

1 回答 1

0

这就是我所做的......这对我有用。

    // global variables
       var db;
       var shortName = 'Books';
       var version = '1.0';
       var displayName = 'BooksDB';
       var maxSize = 200000;//65535;


         function ListDBValues() {

           if (!window.openDatabase) {
               alert('Databases are not supported in this browser.');
               return;
           }

         // this line tries to open the database base locally on the device if it does not exist, it will create it and return a database object stored in variable db


           db = openDatabase(shortName, version, displayName,maxSize);

         // this line clears out any content in the #lbUsers element on the page so that the next few lines will show updated content and not just keep repeating lines

           $('#lbUsers').html('');

         // this next section will select all the content from the User table and then go through it row by row appending the UserId  FirstName  LastName to the #lbUsers element on the page

           db.transaction(function(transaction) {
           transaction.executeSql('SELECT * FROM books;', [], function(transaction, result) { if (result != null && result.rows != null) { for (var i = 0; i < result.rows.length; i++) { var row = result.rows.item(i); $('#lbUsers').append('<br>' + row.book_title + '. ' + row.book_isbn+ ' ' + row.book_price); } } },errorHandler); },errorHandler,nullHandler);

                                   return;
                                   alert('in list end');
                }

        // this is called when a successful transaction happens
        function successCallBack() {
            alert("DEBUGGING: success");

        }

        function nullHandler(){
            alert('null handler');
        };
于 2013-11-04T10:54:40.353 回答