Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a sqlite3 database, using node js, synchronously?

exports.allProbes = function() {     var rows = db.all("SELECT * FROM probes;");     return rows; }; 

main: var json_values = allProbes(); 

Is it possible to do something like that? I mean, without using a callback function: just, read data (sync mode) from the db. and return a json formatted output?

Thanks.

like image 892
Kreshnik Avatar asked Mar 22 '13 16:03

Kreshnik


People also ask

How can I sync two SQLite databases?

You can synchronize SQLite databases by embedding SymmetricDS in your application. It supports occasionally connected clients, so it will capture changes and sync them when a server comes online. It supports several different database platforms and can be used as a library or as a standalone service.

Can I use SQLite with node js?

The sqlite3 module also works with node-webkit if node-webkit contains a supported version of Node. js engine. (See below.) SQLite's SQLCipher extension is also supported.

Can JavaScript connect to a SQLite database?

js and SQLite are separate entities, but both can be used together to create powerful applications. First, we need to link them together by requiring the sqlite3 module. Once we have required this module in our JavaScript, it will give us access to methods to perform various actions with the database.


1 Answers

There are a few npm packages such as better-sqlite3 and sqlite-sync that allow for synchronous SQLite queries. Here's an example:

var sqlite = require("better-sqlite3"); var db = new sqlite("example.db");  var rows = db.prepare("SELECT * FROM probes").all(); console.log(rows); 
like image 179
vqdave Avatar answered Nov 08 '22 13:11

vqdave