MalyG
MalyG

Reputation: 321

Read sqlite3 table with Node.js

I am simply trying to console.log() rows that each have an automatically incremented integer as their primary key. What is the most efficient way to do this in Node.js? My database file, at least for now, consists of one table with a few rows that I need to access. I am very new to sqlite3 and have tried to log

db.run('SELECT * FROM "talon"')

which returns

Database { open: false, filename: 'bin/data.db', mode: 65542 }

Clearly I've not much of an idea what I'm doing, any help is much appreciated!

Upvotes: 0

Views: 189

Answers (1)

Jonathan Apodaca
Jonathan Apodaca

Reputation: 5808

In Node.JS, I/O happens asynchronously. This means that db.run will return execution flow before the DB results are fetched. You will need to provide a callback in order to read the results:

// note, that I changed the call to use "each"
db.each('SELECT * FROM "talon"', function(err, row) {
  if (err) throw err
  console.log(row)
})

I have not tried this code, but I gathered the API details from this link.

Upvotes: 1

Related Questions