简体   繁体   中英

Retrieving Data from existing database using webSQL?

i'm trying to retrieve the data from the database for web application using webSQL ,but i'm unable to get the data from database. I'm very new to this. I tried like this

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

});

Thanks in Advance.

This is how i have done.. and this is working for me.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM