samedi 21 novembre 2015

SQLite Error in Phonegap App

I'm trying to use SQLite database in my Phonegap app. But the app generates an error when I tested on android phone directly. It says "The statement callback raised an exception or statement error callback did not return false". Here's my code for database:

   function init() {
 document.addEventListener("deviceready", onDeviceReady, false);
  }

  function onDeviceReady() {
       DB.transaction(createTableDB, errorCB, successCB);
      $('#save-post').click( function(e){
        e.preventDefault();
        DB.transaction(populateDB, errorCB);
      }

        function createTableDB(tx) {
    tx.executeSql('CREATE TABLE IF NOT EXISTS Posts (id INTEGER PRIMARY KEY AUTOINCREMENT, Title TEXT NOT NULL, Date TEXT NOT NULL, Content TEXT NOT NULL, Link TEXT NOT NULL)');
  }

           function populateDB(tx) {
  var title = document.getElementById('entry-title').innerHTML;
  var date = document.getElementById('entry-date').innerHTML;
  var content = document.getElementById('entry').innerHTML;
  var url = document.getElementById('browser').getAttribute('href');
  tx.executeSql('INSERT INTO Posts(Title,Date,Content,Link) VALUES(?,?,?,?)', [title, date, content, url] );
     }     

      function errorCB(err) {
    alert("Error processing SQL: "+err.message);
    }

        function successCB() {
   DB.transaction(queryDB, errorCB);
      }

      function queryDB(tx){
    tx.executeSql('SELECT * FROM Posts',[],querySuccess,errorCB);
     }

      function querySuccess(tx,result){
    $('#saved-articles-list').empty();
    for( var index=0; index < result.rows.length; index++) {
        var row = result.rows.item(index);
        $('#saved-articles-list').append('<li><a id="'+row['id']+'" data-transition="slide" href="#single"><h1 class="ui-li-heading">'+row['Title']+'</h1><p class="ui-li-desc">'+getDays(row['Date'])+'</p></a></li>');
    $(document).on("click", row['id'], function() {
         showPost(row['Title'], row['Date'], row['Content'], row['Link']);
         });
     }

    $('#saved-articles-list').listview("refresh");
    }

Aucun commentaire:

Enregistrer un commentaire