jeudi 21 avril 2016

How to perform CRUD operations in SQLite plugin cordova in hybrid mobile application?

I have a database and a table in android and have to store data from html form into it.Even installed SQLite plugin of cordova.What should I do next? This is the code integrated in html form.

document.addEventListener('deviceready', onDeviceReady, false);

 // Cordova is ready 
function onDeviceReady() {
  var db = window.sqlitePlugin.openDatabase({name: 'my.db'});
  document.addEventListener('deviceready', onDeviceReady, false);

    // demonstrate PRAGMA:
    db.executeSql("pragma table_info (test_table);", [], function(res) {
        console.log("PRAGMA res: " + JSON.stringify(res));
   });

    tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
      console.log("insertId: " + res.insertId + " -- probably 1");
      console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");

      db.transaction(function(tx) {
        tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
          console.log("res.rows.length: " + res.rows.length + " -- should be 1");
          console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
        });
      });

    }, function(e) {
      console.log("ERROR: " + e.message);
    });
  });
}

}

Aucun commentaire:

Enregistrer un commentaire