Cordova-sqlite-storage: How to set executeSql result value to a variable which is defined as a global variable?

Created on 10 Feb 2016  路  6Comments  路  Source: storesafe/cordova-sqlite-storage

I'm having a problem in assigning a value to my variable which is outside sqLite transaction function. Please take a look at this code.

var map_sync = '';

db.transaction(function(transaction) {
    var select_sql = "SELECT * FROM maps WHERE location_id = "+location_id+" ORDER BY sync_key DESC LIMIT 1"; 

    tx.executeSql(select_sql, [], function(tx, results){
      var len = results.rows.length, iii; console.log("|| Beacons Length ||",len);
      for (iii = 0; iii < len; iii++){
        map_sync = results.rows.item(iii).sync_key;
        console.log("map_sync", map_sync);
      }
    }, function(err){
      map_sync = 0;
      console.log("NodesError:::", err);
    });
});

When i console it inside transaction function it shows me its value. But when i call it outside transaction function it shows me undefined. I know its because of asynchronous behaviour of sqlite. But please anyone give me a way to do it.

Most helpful comment

hi @GopalakrishnanK yes every call is async. You can do with promises or callback

`tx.executeSql(select_sql, [], function(tx, results){ ... your code........ your_callback (results); }

and you need callback function
function your_callback(data) { console.log("here is your data: "+data); }`

okay, and how can i return the value "data" ?
my code looks like this
`function get_config(config_name){

let db = new sqlite3.Database('./db/DudenBot.db', sqlite3.OPEN_READONLY, (err) => {
if (err) {
console.error(err.message);
}
console.log(info.info('INFO') + 'Verbunden mit DudenBot.config Datenbank');
});

db.get("SELECT value FROM config WHERE config = '" + config_name + "'", (err, row) => {
  if (err) {
    console.error(err.message);
  }
  row = row.value;
  return callback(row);
  //console.log(row.value);//works
  //return row.value;//doesnot work
}); 

db.close((err) => {
  if (err) {
    console.error(err.message);
  }
  console.log(info.info('INFO') + 'Schlie脽e DudenBot.config Datenbankverbindung');
});

}
function callback(row) {
var test = "";
test = row;
//console.log(test);
return test;
}
`

All 6 comments

hi @GopalakrishnanK yes every call is async. You can do with promises or callback

tx.executeSql(select_sql, [], function(tx, results){ ... your code........ your_callback (results); }

and you need callback function
function your_callback(data) { console.log("here is your data: "+data); }

Hi @reconka thanks man. I'll try.

I have the same problem, I have a transaction within another, and I need to use the value of a variable of the second transaction in the first.

Now I have the same issue, I'm trying to create an Angular service and I can't find the way to return the results. Example:

angular.module('myapp.services', ['ionic', 'ngCordova'])
.service('myService', function($cordovaSQLite) {

  if (ionic.Platform.isAndroid()) {
    db = $cordovaSQLite.openDB({name: "com.pos.db", iosDatabaseLocation:'default'});
  } else {
    db = $cordovaSQLite.openDB({name: "com.pos.db", location: 2, createFromLocation: 1}); 
  }
  var returnValue;

  db.transaction(function(tx){
    tx.executeSql(<Query>, function(tx, result){
      // Another query
    });
  }, function (error){
    returnValue = false;
  }, function (){
    returnValue = true;
  });
  return returnValue;
});

hi @GopalakrishnanK yes every call is async. You can do with promises or callback

`tx.executeSql(select_sql, [], function(tx, results){ ... your code........ your_callback (results); }

and you need callback function
function your_callback(data) { console.log("here is your data: "+data); }`

okay, and how can i return the value "data" ?
my code looks like this
`function get_config(config_name){

let db = new sqlite3.Database('./db/DudenBot.db', sqlite3.OPEN_READONLY, (err) => {
if (err) {
console.error(err.message);
}
console.log(info.info('INFO') + 'Verbunden mit DudenBot.config Datenbank');
});

db.get("SELECT value FROM config WHERE config = '" + config_name + "'", (err, row) => {
  if (err) {
    console.error(err.message);
  }
  row = row.value;
  return callback(row);
  //console.log(row.value);//works
  //return row.value;//doesnot work
}); 

db.close((err) => {
  if (err) {
    console.error(err.message);
  }
  console.log(info.info('INFO') + 'Schlie脽e DudenBot.config Datenbankverbindung');
});

}
function callback(row) {
var test = "";
test = row;
//console.log(test);
return test;
}
`

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PterPmnta picture PterPmnta  路  4Comments

maoamid picture maoamid  路  4Comments

TommyQu picture TommyQu  路  3Comments

DanielSWolf picture DanielSWolf  路  5Comments

ryaa picture ryaa  路  6Comments