Search code examples
sqlitecordovaandroid-studiocordova-plugins

Sqlite database not created when using android studio


I am trying to make a small app using Cordova. Trying to simply insert some values in the sqlite database. But I am not able to create, nor insert anything. I am very much new to android studio. I tried to run the same code on eclipse and xcode and it worked correctly! I used sqlite-local storage plugin

Here is my Code :

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

function onDeviceReady() {
  var db = window.sqlitePlugin.openDatabase({
    name: "DMS"
  });
  db.transaction(function(tx) {

    // tx.executeSql("DROP TABLE IF  EXISTS tblUserLoginDetail ");
    tx.executeSql("CREATE TABLE IF NOT EXISTS tblUserLoginDetail (UserId INTEGER , Username VARCHAR ,Password VARCHAR )");

    alert("Creating table");
    // tx.executeSql("INSERT INTO  tblUserLoginDetail (UserId,Username,Password) values (1,"kamayani","kam12")",[],function(){alert("success");},function(){alert("error");});

    tx.executeSql('INSERT INTO tblUserLoginDetail (UserId, Username,Password) VALUES ("1", "Firstrow","kkk")');

  });

  $("#btnSignIn").click(function() {
    //
    //ValidateUser();
    alert("onClik");
    var db = window.sqlitePlugin.openDatabase({
      name: "DMS"
    });
    db.transaction(function(tx) {
      tx.executeSql("select * from tblUserLoginDetail", [], function(tx, results) {
        var len = results.rows.length;
        var row = results.rows.item(0);
        var UserDetail = row['UserId'] + "-" + row['Username'];
        alert("MeetingName" + UserDetail);

      }, function() {
        alert("error");
      });


    });
  });
}

function errorCB(err) {
  alert("Error" + err.code);
}

What happens is it shows all the alerts like "Creating table" and then shows the onclick alert .. but then i cannot find the database in the file explorer of Android studio! Am I missing something?

I have added the plugin inside the assets-->www-->plugins folder and given a link to Sqliteplugin.js in the html file.


Solution

  • I know its quiet long to get back. But after spending time on this, i gotta know to that we cannot view .db file straightforward. We gotta root the device or use some other technique. But i came out with a sample app for you which you can download and test it directly after installing the plugin. Please check out this link