Skip to content

Instantly share code, notes, and snippets.

@aweijnitz
Last active February 26, 2017 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aweijnitz/3982587529bc4fd9290251edd6e44f7c to your computer and use it in GitHub Desktop.
Save aweijnitz/3982587529bc4fd9290251edd6e44f7c to your computer and use it in GitHub Desktop.
Example how to use SQLite with Node.js
/**
* Small refresher on how to use sqlite3 embedded in node.js
*
* LINKS:
* - Package: https://www.npmjs.com/package/sqlite3 (NOTE: Includes correct platform binary of sqlite3. No extra install required.)
* - API: https://github.com/mapbox/node-sqlite3/wiki/API
*/
var sqlite3 = require('sqlite3').verbose();
// var db = new sqlite3.Database('./dbfiles/testdb');
var db = new sqlite3.Database(':memory:');
db.serialize(function() {
db.run("CREATE TABLE if not exists lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
console.log("---- db.each")
console.log(row.id + ": " + row.info);
});
db.all("SELECT * from lorem where info like '%7%'",function(err,rows){
console.log("---- db.all")
console.dir(rows);
});
db.get("SELECT * from lorem where info='Ipsum 8'", function (err, row) {
console.log("---- db.get")
console.log(row);
});
});
db.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment