使用webSQL从现有数据库中检索数据?

时间:2013-09-17 05:54:57

标签: javascript database html5 web-sql

我正在尝试使用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);
  }
});

});

先谢谢。

1 个答案:

答案 0 :(得分: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');
        };