2013-09-17 5 views
0

Я пытаюсь получить данные из базы данных для веб-приложения с помощью webSQL, но я не могу получить данные из базы данных. Я очень новичок в этом. Я пробовал вот такИзвлечение данных из существующей базы данных с помощью webSQL?

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.

+0

Что не удается и где ?. отладить его и использовать console.log вместо предупреждения. каковы допустимые имена столбцов в Data? –

ответ

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'); 
     }; 
Смежные вопросы