Skip to content
lovasoa edited this page Jan 1, 2015 · 5 revisions

How to use sql-api.js from nodejs

This example code shows how to open an sqlite3 file on the filesystem, and load it in sql.js

//Node filesystem module - You know that.
var fs = require('fs');

//Ditto, path module
var path = require('path');

//Actual path I'm using to get to sql.js in my project. 
var SQL = require('../js/sql-api.js');

var filebuffer = fs.readFileSync(path.resolve('test.sqlite'));

// Load the db
var db = new SQL.Database(filebuffer);

//[{"columns":["id","content"],"values":[["0","hello"],["1","world"]]}]
console.dir(db.exec("SELECT * FROM test WHERE ID <= 10"));

Note

If your application doesn't need to be usable from a browser or portable, then you might be interested in using node-sqlite3 instead of sql.js. This module includes a native compiled version of sqlite, which is faster than the one compiled to javascript which is included in sql.js.