-
Notifications
You must be signed in to change notification settings - Fork 68
/
index.html
256 lines (236 loc) · 5.43 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<head>
<title>node-sqlite</title>
<style>
pre, code { color: #060; font-size: 11pt; }
pre { margin-left: 2ex; padding: 1ex; background-color: #eee; }
p { font-size: 12pt; }
body {
margin-left: 10%;
margin-right: 10%;
background-color: #fff;
color: black;
max-width: 800px;
}
h1,h2,h3,h4 { font-family: helvetica }
h1 { font-size: 36pt; }
h1 {
background-color: #58b;
color: white;
padding-left:6px;
}
h2 {
color: #28b;
}
</style>
</head>
<body>
<h1>node-sqlite</h1>
<a href=http://sqlite.org/>SQLite</a> bindings for
<a //href=http://nodejs.org/>Node</a>.
<p>
The semantics conform somewhat to those of the <a
href=http://dev.w3.org/html5/webdatabase/#sql>HTML5 Web SQL API</a>,
plus some extensions. Also, only the synchronous API is implemented;
the asynchronous API is a big TODO item.
<h2>Download</h2>
<p>
The spiritual home of node-sqlite is at
<a href=http://grumdrig.github.com/node-sqlite/>
http://grumdrig.github.com/node-sqlite/</a>.
<p>
The code lives at <a href=http://github.com/grumdrig/node-sqlite>
http://github.com/grumdrig/node-sqlite</a>
<style>
pre, code { color: #060; font-size: 11pt; }
pre { margin-left: 2ex; padding: 1ex; background-color: #eee; }
p { font-size: 12pt; }
body {
margin-left: 10%;
margin-right: 10%;
background-color: #fff;
color: black;
max-width: 800px;
}
h1,h2,h3,h4 { font-family: helvetica }
h1 { font-size: 36pt; }
h1 {
background-color: #28b;
color: white;
padding-left:6px;
}
h2 {
color: #28b;
}
</style>
<p>
<h2>Documentation by Example</h2>
<p>
Import the library and open a database. (Only syncronous database
access is implemented at this time.)
<p>
<pre>
var sqlite = require("../sqlite");
var db = sqlite.openDatabaseSync("example.db");
var assert = require("assert").ok;
</pre>
<p>
Perform an SQL query on the database:
<p>
<pre>
db.query("CREATE TABLE foo (a,b,c)");
</pre>
<p>
This is a more convenient form than the HTML5 syntax for the same
thing, but which is also supported:
<p>
<pre>
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE bar (x,y,z)");
});
</pre>
<p>
This allows the same or similar code to work on the client and
server end (modulo browser support of HTML5 Web SQL).
<p>
Transactions generate either a "commit" or "rollback" event.
<p>
<pre>
var rollbacks = 0;
db.addListener("rollback", function () {
++rollbacks;
});
</pre>
<p>
Both forms take an optional second parameter which is values to
bind to fields in the query, as an array:
<p>
<pre>
db.query("INSERT INTO foo (a,b,c) VALUES (?,?,?)", ['apple','banana',22]);
</pre>
<p>
or as a map:
<p>
<pre>
db.query("INSERT INTO bar (x,y,z) VALUES ($x,$y,$zebra)",
{$x: 10, $y:20, $zebra:"stripes"});
</pre>
<p>
Also optional is a callback function which is called with an object
representing the results of the query:
<p>
<pre>
db.query("SELECT x FROM bar", function (records) {
assert(records.length == 1);
assert(records[0].x == 10);
</pre>
<p>
The HTML5 semantics for the record set also work:
<p>
<pre>
assert(records.rows.length == 1);
assert(records.rows.item(0).x == 10);
});
</pre>
<p>
INSERT, UPDATE & DELETE queries set <code>rowsAffected</code> on their result
set object:
<p>
<pre>
db.query("UPDATE foo SET a = ? WHERE a = ?", ['orange', 'apple'], function(r) {
assert(r.rowsAffected == 1);
});
</pre>
<p>
They also emit an <code>"update"</code> event.
<p>
INSERT queries set <code>insertId</code>:
<p>
<pre>
var insert = db.query("INSERT INTO foo VALUES (1,2,3)");
assert(insert.insertId == 2);
</pre>
<p>
Note here that the result set passed to the callback is also
returned by <code>query</code>.
<p>
Multiple-statement queries are supported; each statement's result set is retuned to the callback as a separate parameter:
<p>
<pre>
var q = db.query("UPDATE bar SET z=20; SELECT SUM(z) FROM bar;",
function (update, select) {
assert(update.rowsAffected == 1);
assert(select[0]['SUM(z)'] == 20);
});
</pre>
<p>
An array of all result sets is available as the <code>.all</code> property on
each result set:
<p>
<pre>
assert(q.all[1].length == 1);
</pre>
<p>
HTML5 semantics are supported.
<p>
<pre>
db.transaction(function(tx) {
tx.executeSql("SELECT * FROM foo WHERE c = ?", [3], function(tx,res) {
assert(res.rows.item(0).c == 3);
});
});
</pre>
<p>
The <code>query</code> and <code>transaction</code> APIs wrap lower level APIs that more
thinly wrap the underlying C api:
<p>
<pre>
var stmt = db.prepare("INSERT INTO foo VALUES (?,?,?)");
stmt.bind(1, "curly");
stmt.bind(2, "moe");
stmt.bind(3, "larry");
stmt.step(); // Insert Curly, Moe & Larry
stmt.reset();
stmt.step(); // Insert another row with same stooges
stmt.reset();
stmt.clearBindings();
stmt.bind(2, "lonely");
stmt.step(); // Insert (null, "lonely", null)
stmt.finalize();
</pre>
<p>
Close it:
<p>
<pre>
db.close();
</pre>
<p>
<p>
</pre>
<p>
</pre>
<h2>Installation</h2>
<p>
<ol>
<li>Install <a //href=http://nodejs.org/>Node</a> and
<a href=http://sqlite.org/>SQLite</a>.
<p>
<li>Get the code
<pre>
$ git clone git://github.com/grumdrig/node-sqlite.git
</pre>
<li>Configure and build
<pre>
$ cd node-sqlite
$ node-waf configure
$ node-waf build
</pre>
<li>Test:
<pre>
$ node test.js
$ node doc/examples.js
</pre>
</ol>
<p>
The two files needed to use this library are <code>sqlite.js</code> and
<code>build/default/sqlite3_bindings.node</code>; copy them where you need
them.