mardi 29 mars 2016

cordova sqlite DB issues, can't seem to create/write to tables

My code below is having three specific issues, commented at the bottom of the code. In addition, the console of my browser is showing the DB opening, waiting for transaction and as Opened.

Console Responses:

OPEN database: myapp.db  
CT: registering tables  
new transaction is waiting for open operation  
DB opened: myapp.db  
Avar: 1 
CT: CHECKING myapp table  
Bvar: 1  
A1 Success: {"rows":{"length":0},"rowsAffected":0} 
B1 Success: , {"rows":{"length":0},"rowsAffected":0} 
Error processing SQL: 0, a statement error callback did not return false: no such table: myInfo (code 1): , 
while compiling: SELECT * FROM myInfo
################################################################# 
Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database.     (no such table: myInfo (code 1): , while compiling: SELECT
* FROM myInfo)
#################################################################

function isRegistered() {
  var db = window.sqlitePlugin.openDatabase({name: "my.db"});
  console.log("CT: registering tables") ;
  db.transaction(checkTable, errorCB,function(){
    console.log("Success") ;
  });
}

function isTableExists(tx, tableName, callback) {
  tx.executeSql('SELECT * FROM '+tableName, [], function(tx, resultSet) {
    if (resultSet.rows.length <= 0) {
        callback(false,0);
    } else {
        callback(true,2);
    }
  }, function(err) {
      callback(false,1);
  })
};

function checkTable(tx) {
  // create table
  isTableExists(tx, "myInfo", function(status,avar) {
    console.log("Avar: "+avar) ;
    if (!status) {
      console.log("CT: CHECKING myInfo") ;
      tx.executeSql("CREATE TABLE IF NOT EXISTS myInfo ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,'name' VARCHAR,'phone' VARCHAR,'email' VARCHAR,'fbID' VARCHAR,'fbToken' VARCHAR,'regCode' VARCHAR)",[],function(tx,res) {
        console.log("A1 Success: "+JSON.stringify(res)) ;
        tx.executeSql('INSERT INTO myInfo VALUES (?,?,?,?,?,?,?)',[0,"","","","","",""],function(res) {
          console.log("A2 Success: " + JSON.stringify(res) + " -- probably 1");
        }, function(err) {
          console.log("A2 fail: "+JSON.stringify(err)) ;
        }) ;
      },function(err) {
        console.log("A1 Fail : "+ JSON.stringify(err)) ;
      }) ;
    }
  }, function(err){
    console.log("A Fail: "+JSON.stringify(err)) ;
  }) ;
  isTableExists(tx, "mySettings", function(status,bvar) {
      console.log("Bvar: "+bvar) ;
      if (!status) {
      tx.executeSql('CREATE TABLE IF NOT EXISTS "mySettings" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,"vendor" VARCHAR,"display" VARCHAR,"aggregate" VARCHAR)',[],function(tx,res) {
        console.log("B1 Success: , "+ JSON.stringify(res)) ;
        var vendors = ["General","Twist","Unter","Sobi"] ;
        for (var n=0;n<vendors.length;n++) {
          tx.executeSql('INSERT INTO mySettings (id,vendor,display,aggregate) VALUES (?,?,?,?);',["","","",""],function(res) {
            console.log("B2 Success: " + n + " : " + JSON.stringify(res)) ;
          },function(err) {
            console.log("B2 Fail: "+JSON.stringify(err)) ;
          }) ;
        }        
      },function(err) {
          console.log("B1 Fail: "+JSON.stringify(err)) ;
      }) ;
    }
  }, function(err){
    console.log("B Fail: "+JSON.stringify(err)) ;
  }) ;
}

/* three issues:
1.  everytime I execute the app, it "creates" the tables...it should only do it once.  Avar & Bvar are returning "1" every time
2. The next tx.executeSql("INSERT...is not triggering (that I can tell).
3. Getting general error from isRegistered(...errorCB...); - which I believe is coming from function isTableExists()
Error processing SQL: 0, a statement error callback did not return false: no such table: myInfo (code 1): , while compiling: SELECT * FROM myInfo
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
    (no such table: myInfo (code 1): , while compiling: SELECT * FROM myInfo)
#################################################################
*/

Aucun commentaire:

Enregistrer un commentaire