0 Replies Latest reply on Jun 30, 2016 2:32 AM by samm80030368

    Cordova SQL Lite plugin doesn't work. Window.SQLitePlugin is undefined

    samm80030368

      I'm developping an android app with Cordova 6.2.0 and I want to use the SQLLitePlugin to create databases ... but it doesn't work !

      This is my test code :

       

      <script type="text/javascript">

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

        
      function onDeviceReady() {
        var db
      ;
        alert
      ("window.cordova: " + window.cordova);
        alert
      ("window.SQLitePlugin: " + window.SQLitePlugin);

        
      if (window.cordova && window.SQLitePlugin) {
        db
      = window.sqlitePlugin.openDatabase({ "name": "snet1.db" }); //device - SQLite
        alert
      ("device db (SQLite) loaded");
        
      } else {
        db
      = window.openDatabase("APSNetMobileDb", "1.0", "snet1.db", 100 * 1024 * 1024); // browser webSql, a fall-back for debugging
        alert
      ("browser db (WebSQL) loaded");
        
      }

        db
      .transaction(populateDB, errorCB, successCB);
        
      }

        
      function populateDB(tx) {
        tx
      .executeSql("DROP TABLE IF EXISTS demo");
        tx
      .executeSql('CREATE TABLE IF NOT EXISTS demo (id integer primary key unique, data)');
        tx
      .executeSql('INSERT INTO demo (id, data) VALUES (1, "First row")');
        tx
      .executeSql("INSERT INTO demo (id, data) VALUES (?,?)", [2, 'Second Row']);

        alert
      ("inserted rows");
        queryDB
      (tx);
        
      }

        
      function queryDB(tx) {
        tx
      .executeSql("SELECT id, data FROM demo;", [], querySuccess, errorCB);
        
      }

        
      function querySuccess(tx, results) {
        var len
      = results.rows.length;
        
      for (var i = 0; i < len; i++) {
        document
      .getElementById("output").innerHTML +=
        
      "<table><tr><td>ID = " + results.rows.item(i).id +
        
      "</td><td>data = " + results.rows.item(i).data +
        
      "</td></tr></table>";
        
      }
        
      }

        
      function errorCB() {
        alert
      ("DB access FAILED");
        
      };

        
      function successCB() {
        alert
      ("DB access SUCCEEDED");
        
      };
      </script>

       

      It returns :

      1. windows.SQLitePlugni:undefined
      2. brower db (WEVSQL) loaded
      3. inserted rows
      4. DB access FAILED

       

      I install the plugin from git and I also specify the plugin into my config.xml like that :

       

           <plugin name="cordova-sqlite-ext" spec="https://github.com/litehelpers/cordova-sqlite-ext.git" />

       

      Any issue ? Thanks !

       

      Problem post in stackoverflow two : android - Cordova SQL Lite plugin doesn't work. Window.SQLitePlugin is undefined - Stack Overflow