Skip to content

Commit 8ed88ef

Browse files
authored
Update README.md
1 parent eb15c86 commit 8ed88ef

File tree

1 file changed

+51
-47
lines changed

1 file changed

+51
-47
lines changed

README.md

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,59 +19,63 @@ A [full documentation](http://kripken.github.io/sql.js/documentation/#http://kri
1919

2020
## Usage
2121

22+
By default, *sql.js* uses [wasm](https://developer.mozilla.org/en-US/docs/WebAssembly), and thus needs to load a .wasm file in addition to the javascript library. You can find this file in `./node_modules/sql.js/dist/sql-wasm.wasm` after installing sql.js from npm, and add it to your static assets. Alternatively, you can load the file from a CDN :
23+
2224
```javascript
23-
var initSqlJs = require('sql.js');
25+
const initSqlJs = require('sql.js');
2426
// or if you are in a browser:
2527
// var initSqlJs = window.initSqlJs;
2628

27-
initSqlJs().then(SQL => {
28-
29-
// Create a database
30-
var db = new SQL.Database();
31-
// NOTE: You can also use new SQL.Database(data) where
32-
// data is an Uint8Array representing an SQLite database file
33-
34-
// Execute some sql
35-
sqlstr = "CREATE TABLE hello (a int, b char);";
36-
sqlstr += "INSERT INTO hello VALUES (0, 'hello');"
37-
sqlstr += "INSERT INTO hello VALUES (1, 'world');"
38-
db.run(sqlstr); // Run the query without returning anything
39-
40-
var res = db.exec("SELECT * FROM hello");
41-
/*
42-
[
43-
{columns:['a','b'], values:[[0,'hello'],[1,'world']]}
44-
]
45-
*/
46-
47-
// Prepare an sql statement
48-
var stmt = db.prepare("SELECT * FROM hello WHERE a=:aval AND b=:bval");
49-
50-
// Bind values to the parameters and fetch the results of the query
51-
var result = stmt.getAsObject({':aval' : 1, ':bval' : 'world'});
52-
console.log(result); // Will print {a:1, b:'world'}
53-
54-
// Bind other values
55-
stmt.bind([0, 'hello']);
56-
while (stmt.step()) console.log(stmt.get()); // Will print [0, 'hello']
57-
58-
// You can also use JavaScript functions inside your SQL code
59-
// Create the js function you need
60-
function add(a, b) {return a+b;}
61-
// Specifies the SQL function's name, the number of it's arguments, and the js function to use
62-
db.create_function("add_js", add);
63-
// Run a query in which the function is used
64-
db.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));"); // Inserts 10 and 'Hello world'
65-
66-
// free the memory used by the statement
67-
stmt.free();
68-
// You can not use your statement anymore once it has been freed.
69-
// But not freeing your statements causes memory leaks. You don't want that.
70-
71-
// Export the database to an Uint8Array containing the SQLite database file
72-
var binaryArray = db.export();
29+
const SQL = await initSqlJs({
30+
// Required to load the wasm binary asynchronously. Of course, you can host it wherever you want
31+
// You can omit locateFile completely when running in node
32+
locateFile: file => `https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.1.0/dist/${file}`
7333
});
7434

35+
// Create a database
36+
var db = new SQL.Database();
37+
// NOTE: You can also use new SQL.Database(data) where
38+
// data is an Uint8Array representing an SQLite database file
39+
40+
// Execute some sql
41+
sqlstr = "CREATE TABLE hello (a int, b char);";
42+
sqlstr += "INSERT INTO hello VALUES (0, 'hello');"
43+
sqlstr += "INSERT INTO hello VALUES (1, 'world');"
44+
db.run(sqlstr); // Run the query without returning anything
45+
46+
var res = db.exec("SELECT * FROM hello");
47+
/*
48+
[
49+
{columns:['a','b'], values:[[0,'hello'],[1,'world']]}
50+
]
51+
*/
52+
53+
// Prepare an sql statement
54+
var stmt = db.prepare("SELECT * FROM hello WHERE a=:aval AND b=:bval");
55+
56+
// Bind values to the parameters and fetch the results of the query
57+
var result = stmt.getAsObject({':aval' : 1, ':bval' : 'world'});
58+
console.log(result); // Will print {a:1, b:'world'}
59+
60+
// Bind other values
61+
stmt.bind([0, 'hello']);
62+
while (stmt.step()) console.log(stmt.get()); // Will print [0, 'hello']
63+
64+
// You can also use JavaScript functions inside your SQL code
65+
// Create the js function you need
66+
function add(a, b) {return a+b;}
67+
// Specifies the SQL function's name, the number of it's arguments, and the js function to use
68+
db.create_function("add_js", add);
69+
// Run a query in which the function is used
70+
db.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));"); // Inserts 10 and 'Hello world'
71+
72+
// free the memory used by the statement
73+
stmt.free();
74+
// You can not use your statement anymore once it has been freed.
75+
// But not freeing your statements causes memory leaks. You don't want that.
76+
77+
// Export the database to an Uint8Array containing the SQLite database file
78+
var binaryArray = db.export();
7579
```
7680

7781
## Demo

0 commit comments

Comments
 (0)