jeudi 5 mai 2016

Return Num of rows Sqlite

I had a question concerning returning number of rows from sqlite query inside the db.serialize function

here is my function:

var rowscount =0;
    function DatabaseDictionary(searchword){

    var fs = require("fs");
    var file = "test.db";
    var exists = fs.existsSync(file);

    if(!exists) {
      console.log("Creating DB file.");
      fs.openSync(file, "w");
      }

     var sqlite3 = require("sqlite3").verbose();
     var db = new sqlite3.Database(file);
     var rowscount =0;
     db.serialize(function() {
     if(!exists) {
        db.run("CREATE TABLE entries (word,wordtype,definition)");
      }
          db.each("SELECT COUNT(*) AS wordsCount FROM entries where word='"+searchword + "'", function(err, row) { 
          console.log('output db ' + row.wordsCount);
          rowscount =  row.wordsCount;
        });
         
      });
       

    db.close();

    }

what i want to know is HOW TO RETURN THE variable rowscount to use it afterwards, i'm not capable of doing this and i tried a lot.

Aucun commentaire:

Enregistrer un commentaire