You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+95-47Lines changed: 95 additions & 47 deletions
Original file line number
Diff line number
Diff line change
@@ -3,21 +3,23 @@
3
3
4
4
For the impatients, try the demo here: http://kripken.github.io/sql.js/GUI/
5
5
6
-
*sql.js* is a port of [SQLite](http://sqlite.org/about.html) to JavaScript, by compiling the SQLite C code with [Emscripten](http://kripken.github.io/emscripten-site/docs/introducing_emscripten/about_emscripten.html). It uses a [virtual database file stored in memory](https://kripken.github.io/emscripten-site/docs/porting/files/file_systems_overview.html), and thus **doesn't persist the changes** made to the database. However, it allows you to **import** any existing sqlite file, and to **export** the created database as a [javascript typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays).
6
+
*sql.js* is a port of [SQLite](http://sqlite.org/about.html) to Webassembly, by compiling the SQLite C code with [Emscripten](http://kripken.github.io/emscripten-site/docs/introducing_emscripten/about_emscripten.html). It uses a [virtual database file stored in memory](https://kripken.github.io/emscripten-site/docs/porting/files/file_systems_overview.html), and thus **doesn't persist the changes** made to the database. However, it allows you to **import** any existing sqlite file, and to **export** the created database as a [javascript typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays).
7
7
8
-
There is no C bindings or node-gyp compilation here, sql.js is a simple javascript file, that can be used like any traditional javascript library. If you are building a native application in javascript (using Electron for instance), or are working in node.js, you will likely prefer to use [a native binding of SQLite to javascript](https://www.npmjs.com/package/sqlite3).
8
+
There are no C bindings or node-gyp compilation here, sql.js is a simple javascript file, that can be used like any traditional javascript library. If you are building a native application in javascript (using Electron for instance), or are working in node.js, you will likely prefer to use [a native binding of SQLite to javascript](https://www.npmjs.com/package/sqlite3).
9
9
10
10
SQLite is public domain, sql.js is MIT licensed.
11
11
12
+
Sql.js predates WebAssembly, and thus started as an [asm.js](https://en.wikipedia.org/wiki/Asm.js) project. It still supports asm.js for backwards compatability.
13
+
12
14
## Documentation
13
15
A [full documentation](http://kripken.github.io/sql.js/documentation/#http://kripken.github.io/sql.js/documentation/class/Database.html) generated from comments inside the source code, is available.
14
16
15
17
## Usage
16
18
17
19
```javascript
18
20
var initSqlJs =require('sql-wasm.js');
19
-
20
-
// or initSqlJs = window.initSqlJs if you are in a browser
There is an online demo available here : http://kripken.github.io/sql.js/GUI
75
+
There are a few examples [available here](https://kripken.github.io/sql.js/index.html). The most full-featured is the [Sqlite Interpreter](https://kripken.github.io/sql.js/examples/GUI/index.html).
74
76
75
77
## Examples
76
78
The test files provide up to date example of the use of the api.
77
79
### Inside the browser
78
80
#### Example **HTML** file:
79
81
```html
80
-
<scriptsrc='dist/sql-wasm.js'></script>
81
-
<script>
82
-
//the `initSqlJs` function is globally provided by all of the main dist files if loaded in the browser.
83
-
initSqlJs().then(function(SQL){
84
-
//Create the database
85
-
var db =newSQL.Database();
86
-
// Run a query without reading the results
87
-
db.run("CREATE TABLE test (col1, col2);");
88
-
// Insert two rows: (1,111) and (2,222)
89
-
db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,222]);
90
-
91
-
// Prepare a statement
92
-
var stmt =db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
`sql.js` is [hosted on npm](https://www.npmjs.org/package/sql.js). To install it, you can simply run `npm install sql.js`.
142
-
Alternatively, you can simply download the file`sql.js`, from the download link below.
155
+
Alternatively, you can simply download `sql-wasm.js` and`sql-wasm.wasm`, from the download link below.
143
156
144
157
#### read a database from the disk:
145
158
```javascript
146
159
var fs =require('fs');
147
160
var initSqlJs =require('sql-wasm.js');
148
161
var filebuffer =fs.readFileSync('test.sqlite');
149
162
150
-
initSqlJs().then(function(SqlJs){
163
+
initSqlJs().then(function(SQL){
151
164
// Load the db
152
-
var db =newSqlJs.Database(filebuffer);
165
+
var db =newSQL.Database(filebuffer);
153
166
});
154
167
155
168
```
@@ -170,12 +183,12 @@ See : https://github.com/kripken/sql.js/blob/master/test/test_node_file.js
170
183
If you don't want to run CPU-intensive SQL queries in your main application thread,
171
184
you can use the *more limited* WebWorker API.
172
185
173
-
You will need to download `worker.sql-wasm.js`
186
+
You will need to download [dist/worker.sql-wasm.js](dist/worker.sql-wasm.js)[dist/worker.sql-wasm.wasm](dist/worker.sql-wasm.wasm).
174
187
175
188
Example:
176
189
```html
177
190
<script>
178
-
var worker =newWorker("dist/worker.sql-wasm.js");// You can find worker.sql.js in this repo
191
+
var worker =newWorker("/dist/worker.sql-wasm.js");
179
192
worker.onmessage= () => {
180
193
console.log("Database opened");
181
194
worker.onmessage=event=> {
@@ -198,35 +211,70 @@ Example:
198
211
</script>
199
212
```
200
213
201
-
See : https://github.com/kripken/sql.js/blob/master/test/test_worker.js
214
+
See [examples/GUI/gui.js](examples/GUI/gui.js) for a full working example.
202
215
203
216
## Flavors/versions Targets/Downloads
204
217
205
-
This library includes both asm.js and WebAssembly versions of Sqlite. (WebAssembly is the newer, preferred way to compile to Javascript, and has superceded asm.js. It produces smaller, faster code.) Asm.js versions are included for compatibility.
218
+
This library includes both WebAssembly and asm.js versions of Sqlite. (WebAssembly is the newer, preferred way to compile to Javascript, and has superceded asm.js. It produces smaller, faster code.) Asm.js versions are included for compatibility.
219
+
220
+
## Upgrading from 0.x to 1.x
221
+
222
+
Version 1.0 of sql.js introduces a number of breaking changes due primarily to the fact that WebAssembly must be loaded asynchronously, whereas asm.js was able to be loaded synchronously.
Version 1.0 of sql.js introduces a number of breaking changes due primarily to the fact that WASM must be loaded asynchronously, whereas asm.js was able to be loaded synchronously.
210
261
211
-
TODO: More info here:
262
+
### Downloading/Using: ###
263
+
Although asm.js files were distributed as a single Javascript file, WebAssembly libraries are most efficiently distributed as a pair of files, the `.js` loader and the `.wasm` file, like [dist/sql-wasm.js]([dist/sql-wasm.js]) and [dist/sql-wasm.wasm]([dist/sql-wasm.wasm]). The `.js` file is reponsible for wrapping/loading the `.wasm` file.
264
+
265
+
212
266
213
267
214
268
## Versions of sql.js included in `dist/`
215
269
-`sql-wasm.js` : The Web Assembly version of Sql.js. Minified and suitable for production. Use this. If you use this, you will need to include/ship `sql-wasm.wasm` as well.
216
270
-`sql-wasm-debug.js` : The Web Assembly, Debug version of Sql.js. Larger, with assertions turned on. Useful for local development. You will need to include/ship `sql-wasm-debug.wasm` if you use this.
217
271
-`sql-asm.js` : The older asm.js version of Sql.js. Slower and larger. Provided for compatiblity reasons.
218
272
-`sql-asm-memory-growth.js` : Asm.js doesn't allow for memory to grow by default, because it is slower and de-optimizes. If you are using sql-asm.js and you see this error (`Cannot enlarge memory arrays`), use this file.
219
-
- `sql-asm-debug.js` : The _Debug_ asm.js version of Sql.js. If using sql-asm.js, use this for local development.
220
-
- `worker.*` - Web Worker versions of the above libraries
221
-
Asm.js builds are included for backwards compatilbility.
273
+
-`sql-asm-debug.js` : The _Debug_ asm.js version of Sql.js. Use this for local development.
274
+
-`worker.*` - Web Worker versions of the above libraries. More limited API. See [examples/GUI/gui.js](examples/GUI/gui.js) for a good example of this.
222
275
223
276
## Compiling
224
277
225
278
- Install the EMSDK, [as described here](https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html)
0 commit comments