Commit 4bddfa78 by Orlando Vazquez

Clean up README

parent 6d53991a
...@@ -2,24 +2,14 @@ ...@@ -2,24 +2,14 @@
node-sqlite - Asynchronous SQLite3 driver for Node.js node-sqlite - Asynchronous SQLite3 driver for Node.js
This distribution includes two SQLite libraries: a low level driver
written in C++ and a high level driver. The latter wraps the former to add
simpler API.
SQLite calls block, so to work around this, synchronous calls happen within SQLite calls block, so to work around this, synchronous calls happen within
Node's libeio thread-pool, in a similar manner to how POSIX calls are Node's libeio thread-pool, in a similar manner to how POSIX calls are
currently made. SQLite's serialized threading mode is used to make sure we currently made.
use SQLite safely. See http://www.sqlite.org/threadsafe.html for more info.
# SYNOPSIS # SYNOPSIS
## High-level Driver ## High-level Driver
High-level bindings provide a simple interface to SQLite3. They should be
fast enough for most purposes, but if you absolutely need more performance,
the low level drivers are also straight-forward to use, but require a few
additional steps.
var sys = require('sys'), var sys = require('sys'),
sqlite = require('sqlite'); sqlite = require('sqlite');
...@@ -28,66 +18,21 @@ additional steps. ...@@ -28,66 +18,21 @@ additional steps.
// open the database for reading if file exists // open the database for reading if file exists
// create new database file if not // create new database file if not
db.open("lilponies.db", function () { db.open("aquateen.db", function (error) {
var colour = 'pink'; if (error) {
var sql = 'SELECT name FROM ponies WHERE hair_colour = ?'; console.log("Purple Alert! Aqua Teen Database unabled to be opened!"));
throw error;
// bindings list is optional
var ponies = [];
db.query(sql, [colour], function (error, pony) {
if (error) throw error;
if (!pony) {
// no more ponies
if (!ponies.length)
sys.puts('There are no ponies with ' + colour + ' tails. :(');
else
sys.puts('The following ponies have ' + colour + ' tails: ' + ponies.join(', '));
} }
sys.puts(sys.inspect(pony)); var sql = 'SELECT name FROM dudes WHERE type = ? AND age > ?';
ponies.push(pony);
});
});
## Low-level Driver
The low-level bindings directly interface with the SQLite C API. The API
approximately matches the SQLite3 API when it makes sense.
var sys = require('sys'),
sqlite = require('sqlite');
var db = new sqlite.Database();
// open the database for reading if file exists
// create new database file if not
db.open("lilponies.db", function () {
var colour = 'pink';
var sql = 'SELECT name FROM ponies' +
' WHERE hair_colour = $hair_colour' +
' AND gemstones = ?';
var ponies = [];
db.prepare(sql, function (error, statement) { db.prepare(sql, function (error, statement) {
if (error) throw error; if (error) throw error;
// Fill in the placeholders // Fill in the placeholders
// Could also have used: statement.bindArray(['milkshake', 30], function () {
// statement.bind(position, value, function () { ... });
// statement.bindObject({ $hair_colour: 'pink' }, function () {});
statement.bindArray(['pink', 4], function () {
// call step once per row result
statement.step(function (error, row) {
if (!row) {
// end of rows
}
// do some stuff statement.fetchAll(function (error, rows) {
// call statement.step() again for next row // ...
}); });
}); });
}); });
...@@ -98,6 +43,7 @@ approximately matches the SQLite3 API when it makes sense. ...@@ -98,6 +43,7 @@ approximately matches the SQLite3 API when it makes sense.
## Database Objects ## Database Objects
To create a new database object: To create a new database object:
var sqlite = require('sqlite');
var db = sqlite.Database(); var db = sqlite.Database();
...@@ -113,6 +59,12 @@ A filename of ":memory:" may be used to create an in-memory database. ...@@ -113,6 +59,12 @@ A filename of ":memory:" may be used to create an in-memory database.
Close the database handle. Close the database handle.
### database.query(sql, [bindings,] function (error, row) {})
Execute a SQL query, `sql`, with optional bindings `bindings` on the currently
opened database. The callback will be executed once per row returned, plus
once more with row set to undefined to indicate end of results.
### database.executeScript(SQL, function (error) {}); ### database.executeScript(SQL, function (error) {});
db.executeScript db.executeScript
...@@ -126,9 +78,8 @@ Close the database handle. ...@@ -126,9 +78,8 @@ Close the database handle.
}); });
Execute multiple semi-colon separated SQL statements. Statements must take no Execute multiple semi-colon separated SQL statements. Statements must take no
placeholders. Each statements will be executed with a single step() and then placeholders. Each statement will be executed with a single step() and then
reset. This is ideally suited to executing DDL statements that take no reset. This is ideally suited to executing multiple DDL statements.
arguments and return no results.
### database.prepare(SQL, [options,] function (error, statement) {}) ### database.prepare(SQL, [options,] function (error, statement) {})
...@@ -139,12 +90,12 @@ is performed. ...@@ -139,12 +90,12 @@ is performed.
Options: Options:
- lastInsertRowID: boolean, default false. - lastInsertRowID: boolean, default false.
If true, when this statement is stepped over, the context object (this) in If true, when this statement is step()'d over, the context object (this) in
the callback will contain a lastInsertRowID member with the ID of the last the callback will contain a lastInsertRowID member with the ID of the last
inserted row. inserted row.
- affectedRows: boolean, default false. - affectedRows: boolean, default false.
If true, when this statement is stepped over, the context object (this) in If true, when this statement is step()'d over, the context object (this) in
the callback will contain an affectedRows member with the number of the callback will contain an affectedRows member with the number of
affected rows for the last step. affected rows for the last step.
...@@ -180,8 +131,7 @@ Immediately clear the bindings from the statement. There is no callback. ...@@ -180,8 +131,7 @@ Immediately clear the bindings from the statement. There is no callback.
Fetch one row from a prepared statement and hand it off to a callback. If Fetch one row from a prepared statement and hand it off to a callback. If
there are no more rows to be fetched, row will be undefined. Rows are there are no more rows to be fetched, row will be undefined. Rows are
represented as objects with represented as objects with properties named after the respective columns.
properties named after the respective columns.
### statement.fetchAll(function (error, rows) {}) ### statement.fetchAll(function (error, rows) {})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment