Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Node.js, Express, and SQLite to wrap a REST API around an SQL database
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('data/demodb02');
db.serialize(function() {"CREATE TABLE IF NOT EXISTS counts (key TEXT, value INTEGER)");"INSERT INTO counts (key, value) VALUES (?, ?)", "counter", 0);
var express = require('express');
var restapi = express();
restapi.get('/data', function(req, res){
db.get("SELECT value FROM counts", function(err, row){
res.json({ "count" : row.value });
});'/data', function(req, res){"UPDATE counts SET value = value + 1 WHERE key = ?", "counter", function(err, row){
if (err){
else {
console.log("Submit GET or POST to http://localhost:3000/data");
Copy link

koalahamlet commented Aug 5, 2014

@dalelane I get this error

throw er; // Unhandled 'error' event
Error: SQLITE_CANTOPEN: unable to open database file

Copy link

LuizPanariello commented Aug 14, 2014

You can try to use 'fs' (file system) to read the file and pass it to sqlite3.

Copy link

seddik commented Sep 14, 2016


var path = require('path');
var dbPath = path.resolve(__dirname, 'mydb.db')

Copy link

jstipsBR commented Oct 31, 2016

how i make to get and update all rows ???

Copy link

quetzaluz commented Sep 10, 2018

how i make to get and update all rows ???

Using db.all can query all rows:

Using is probably your best bet for any update statements:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment