SQLite compiled to javascript
This is an experimental build of SQLite4 to javascript. SQLite4 itself is itself still experimental and not well documented.
That said, this build seems to work well (except for the known bug listed below), and is much faster than SQLite3. On my computer, it loads faster, and runs at least 3 times faster than sqlite3.
sql.js is a port of SQLite to JavaScript, by compiling the SQLite C code with Emscripten. no C bindings or node-gyp compilation here.
SQLite is public domain, sql.js is MIT licensed.
Known Bugs
Database.export()
returns an empty file (a large Uint8Array containing only zeroes)
Usage
var sql = ;// or sql = window.SQL if you are in a browser // Create a databasevar db = ;// NOTE: You can also use new sql.Database(data) where// data is an Uint8Array representing an SQLite database file // Execute some sqlsqlstr = "CREATE TABLE hello (a int, b char);";sqlstr += "INSERT INTO hello VALUES (0, 'hello');"sqlstr += "INSERT INTO hello VALUES (1, 'world');"db; // Run the query without returning anything var res = db;/*[ {columns:['a','b'], values:[[0,'hello'],[1,'world']]}]*/ // Prepare an sql statementvar stmt = db; // Bind values to the parameters and fetch the results of the queryvar result = stmt;console; // Will print {a:1, b:'world'} // Bind other valuesstmt;while stmt console; // Will print [0, 'hello'] // free the memory used by the statementstmt;// You can not use your statement anymore once it has been freed.// But not freeing your statements causes memory leaks. You don't want that. // Export the database to an Uint8Array containing the SQLite database filevar binaryArray = db;
Demo
There is an online demo available here : http://lovasoa.github.io/sql.js/GUI
Exemples
The test files provide up to date example of the use of the api.
Inside the browser
Example HTML file:
Creating a database from a file choosen by the user
SQL.Database
constructor takes an array of integer representing a database file as an optional parameter.
The following code uses an HTML input as the source for loading a database:
dbFileElm { var f = dbFileElmfiles0; var r = ; r { var Uints = rresult; db = Uints; } r;}
See : http://lovasoa.github.io/sql.js/GUI/gui.js
Use from node.js
sql.js
is hosted on npm. To install it, you can simply run npm install sql.js
.
Alternatively, you can simply download the file sql.js
, from the download link below.
read a database from the disk:
var fs = ;var SQL = ;var filebuffer = fs; // Load the dbvar db = filebuffer;
Write a database to the disk
You need to convert the result of db.export
to a buffer
var fs = ;// [...] (create the database)var data = db;var buffer = data;fs;
See : https://github.com/lovasoa/sql.js/blob/master/test/test_node_file.js
Use as web worker
If you don't want to run CPU-intensive SQL queries in your main application thread, you can use the more limited WebWorker API.
You will need to download worker.sql.js
Example:
See : https://github.com/lovasoa/sql.js/blob/master/test/test_worker.js
Documentation
The API is fully documented here : http://lovasoa.github.io/sql.js/documentation/
Download
You can download sql.js
here : http://lovasoa.github.io/sql.js/js/sql.js
And the Web Worker version: http://lovasoa.github.io/sql.js/js/worker.sql.js
Differences from the original sql.js
- Support for BLOBs
- Support for prepared statements
- Cleaner API
- More recent version of SQLite (3.8.4)
- Compiled to asm.js (should be faster, at least on firefox)
- Changed API. Results now have the form
[{'columns':[], values:[]}]
- Improved GUI of the demo. It now has :
- syntax highlighting
- nice HTML tables to display results
- ability to load and save sqlite database files