|
| 1 | +--- |
| 2 | +layout: recipe |
| 3 | +title: SQLite |
| 4 | +chapter: Databases |
| 5 | +--- |
| 6 | +## Problem |
| 7 | + |
| 8 | +You need to interface with a [SQLite](http://www.sqlite.org/) database from inside of Node.js. |
| 9 | + |
| 10 | +## Solution |
| 11 | + |
| 12 | +Use the [SQLite module](http://code.google.com/p/node-sqlite/). |
| 13 | + |
| 14 | +{% highlight coffeescript %} |
| 15 | +sqlite = require 'sqlite' |
| 16 | + |
| 17 | +db = new sqlite.Database |
| 18 | + |
| 19 | +# The module uses asynchronous methods, |
| 20 | +# so we chain the calls the db.execute |
| 21 | +exampleCreate = -> |
| 22 | + db.execute "CREATE TABLE snacks (name TEXT(25), flavor TEXT(25))", |
| 23 | + (exeErr, rows) -> |
| 24 | + throw exeErr if exeErr |
| 25 | + exampleInsert() |
| 26 | + |
| 27 | +exampleInsert = -> |
| 28 | + db.execute "INSERT INTO snacks (name, flavor) VALUES ($name, $flavor)", |
| 29 | + { $name: "Potato Chips", $flavor: "BBQ" }, |
| 30 | + (exeErr, rows) -> |
| 31 | + throw exeErr if exeErr |
| 32 | + exampleSelect() |
| 33 | + |
| 34 | +exampleSelect = -> |
| 35 | + db.execute "SELECT name, flavor FROM snacks", |
| 36 | + (exeErr, rows) -> |
| 37 | + throw exeErr if exeErr |
| 38 | + console.log rows[0] # => { name: 'Potato Chips', flavor: 'BBQ' } |
| 39 | + |
| 40 | +# :memory: creates a DB in RAM |
| 41 | +# You can supply a filepath (like './example.sqlite') to create/open one on disk |
| 42 | +db.open ":memory:", (openErr) -> |
| 43 | + throw openErr if openErr |
| 44 | + exampleCreate() |
| 45 | +{% endhighlight %} |
| 46 | + |
| 47 | +## Discussion |
| 48 | + |
| 49 | +You can also prepare your SQL queries beforehand: |
| 50 | + |
| 51 | +{% highlight coffeescript %} |
| 52 | +sqlite = require 'sqlite' |
| 53 | +async = require 'async' # Not required but added to make the example more concise |
| 54 | + |
| 55 | +db = new sqlite.Database |
| 56 | + |
| 57 | +createSQL = "CREATE TABLE drinks (name TEXT(25), price NUM)" |
| 58 | + |
| 59 | +insertSQL = "INSERT INTO drinks (name, price) VALUES (?, ?)" |
| 60 | + |
| 61 | +selectSQL = "SELECT name, price FROM drinks WHERE price < ?" |
| 62 | + |
| 63 | +create = (onFinish) -> |
| 64 | + db.execute createSQL, (exeErr) -> |
| 65 | + throw exeErr if exeErr |
| 66 | + onFinish() |
| 67 | + |
| 68 | +prepareInsert = (name, price, onFinish) -> |
| 69 | + db.prepare insertSQL, (prepErr, statement) -> |
| 70 | + statement.bindArray [name, price], (bindErr) -> |
| 71 | + statement.fetchAll (fetchErr, rows) -> # Called so that it executes the insert |
| 72 | + onFinish() |
| 73 | + |
| 74 | +prepareSelect = (onFinish) -> |
| 75 | + db.prepare selectSQL, (prepErr, statement) -> |
| 76 | + statement.bindArray [1.00], (bindErr) -> |
| 77 | + statement.fetchAll (fetchErr, rows) -> |
| 78 | + console.log rows[0] # => { name: "Mia's Root Beer", price: 0.75 } |
| 79 | + onFinish() |
| 80 | + |
| 81 | +db.open ":memory:", (openErr) -> |
| 82 | + async.series([ |
| 83 | + (onFinish) -> create onFinish, |
| 84 | + (onFinish) -> prepareInsert "LunaSqueeze", 7.95, onFinish, |
| 85 | + (onFinish) -> prepareInsert "Viking Sparkling Grog", 4.00, onFinish, |
| 86 | + (onFinish) -> prepareInsert "Mia's Root Beer", 0.75, onFinish, |
| 87 | + (onFinish) -> prepareSelect onFinish |
| 88 | + ]) |
| 89 | +{% endhighlight %} |
| 90 | + |
| 91 | +The [SQLite version of SQL](http://www.sqlite.org/lang.html) and the [node-sqlite module documentation](https://github.com/orlandov/node-sqlite#readme) provide more complete information. |
| 92 | + |
0 commit comments