简体   繁体   中英

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM