1

I am using this pakcage: sqlite3 to manipulate database queries, code like this:

single query is quit simple:

 var sqlite3 = require('sqlite3').verbose();
 let db = new sqlite3.Database('database.sqlite3');
 let entity_1_name = ''
 db.serialize(function(){
   db.get('select * from table1 where id = 1', function(error, row){
      entity_1_name = row.name
   })
 })
 db.close()

But how to execute multiple queries and in sequence? (e.g. I want to run query1, then query2, then query3 )
just like:

 var sqlite3 = require('sqlite3').verbose();
 let db = new sqlite3.Database('database.sqlite3');
 let entity_1_name = ''
 let entity_2_name = ''
 db.serialize(function(){

   // this is query1
   db.get('select * from table1 where id = 1', [], function(error, row){
      entity_1_name = row.name
   })

   // this is query2
   db.get('select * from table2 where dependency_column_name = ' + entity_1_name, function(error,row){
      // this sql returns null value, because when this query was executed, the variable "entity_1_name" was not determined by query1.
   })

 })


 db.close()

6
  • 1
    After quite a bit of experimentation, I was only able to get this to work if I put the second query inside the callback of the first query. Commented Jun 7, 2021 at 1:52
  • Yeah, it's easy to get into the callback hell... Commented Jun 7, 2021 at 2:27
  • Seems very easy. I'm surprised they don't have any query methods which return something other than the DB object. I might look at writing an async/await wrapper around this package, cause I'd want that functionality myself. Commented Jun 7, 2021 at 2:29
  • Yeah, if you got async/await work, please let me know~ thanks. Commented Jun 7, 2021 at 2:31
  • 1
    @Siwei sqlite wraps it in TS and knex imo is a bit easier to work with Commented Jun 7, 2021 at 5:19

2 Answers 2

2

OK, at last, I used promise + async + await to make it done.

Step1. define a function return new Promise()

      do_query_1(material_name){
        // 1. return this new Promise() 
        return new Promise( (resolve, reject) => {
          let db = this.$database_tool.get_db()
          let that = this
          db.serialize(function(){
            db.get(`select * from table1 where id = 1`, [], function(error, row){
              // 2. this is the point, put anything you want to result to resolve()
              resolve(row.id)
            })
          })
          db.close();

        })
      },

Step2. define a async function, to call the method defined in Step1.


      async do_query_2(){
        let that = this
        // other lines was omitted.
        // HERE , use await to get the result from STEP1. 
        let entity_1_name = await that.do_query_1(row.material_name)

        db.serialize(function(){
          let sql = `'select * from table2 where dependency_column_name = ' + entity_1_name`

          db.run(sql)
        })
        db.close();
      },

Step3. call do_query_2() in normal way:

do_query_2()

That's it!

Sign up to request clarification or add additional context in comments.

Comments

1

https://stackoverflow.com/a/40903302/16140221 May help you out

Their solution was to place each query within a function, then simply calling them. (This is directly the code taken from the answer, it is not mine.)

function db_query1(your_param,...., callback){
  // database operation
   db.run( sql , [param,...] , function(err,rows){
      if(err) // return
      else{
         // get rows with callback
         callback(null, rows);
      }
   });
}

function db_query2(your_param,...., callback){
  // database operation
   db.run( sql , [param,...] , function(err,rows){
      if(err) // return
      else{
         // get rows with callback
         callback(null, rows);
      }
   });
}

Then calling the functions

db_query1(....,function(err,result1){
   if(err) ...// return 
   // do the things with result1
   // And then call query2
   db_query2(....,function(err,result2){
      if(err) ...// return 
      // do the things with result1
   });
});

2 Comments

Thanks, your solution is the last choice. It's OK to go, but very easy to fall into the "call back hell".
Call back hell... Not a good idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.