Last active
February 26, 2017 19:12
-
-
Save aweijnitz/3982587529bc4fd9290251edd6e44f7c to your computer and use it in GitHub Desktop.
Example how to use SQLite with Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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