O
O
okso2019-09-21 09:11:22
Node.js
okso, 2019-09-21 09:11:22

Node.js + node-sqlite3. How to send a generated request?

I make a query to the database through the node-sqlite3 module.
Code part:

var rowPool = [];
var rowPool2 = [{id:0}];
var rowPool3 = [];
var select='SELECT rowid AS id, title, kod FROM telefon_ron';
var select2='SELECT rowid AS id, title FROM telefon_otdel WHERE ron_id=1';

db.serialize(function(){

  db.all(select, function(err, row) {
    rowPool=row;
    console.log(rowPool); //Выводит содержимое
  });

  db.all(select2, function(err, row) {
    rowPool2=row;
    console.log(rowPool2); //Выводит содержимое
    console.log(rowPool2[0].id); //Выводит: 4
    //select3=`SELECT rowid AS id, title FROM telefon_potdel WHERE otdel_id=${rowPool2[0].id}`;
  });

   db.all(`SELECT rowid AS id, title FROM telefon_potdel WHERE otdel_id=${rowPool2[0].id}`, function(err, row) {
     console.log(rowPool2[0].id); //Выводит: 4
     rowPool3=row;
     console.log(rowPool3);  //Выводит пустой массив: []
   });

});

Why doesn't the query in the last db.all() work? How to fix?
If at the very beginning we declare var rowPool2 = [{id:4}], then console.log(rowPool3) will display the contents of the database based on the passed request.
If the last db.all() is passed not a string, but a select3 variable, then it will display an error:
TypeError: SQL query expected

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question