Skip to content

Commit 7bdff4d

Browse files
author
kai.zhu
committed
- add documentation
1 parent 632effa commit 7bdff4d

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,34 @@ Example:
204204
205205
worker.postMessage({
206206
id: 2,
207-
action: 'exec',
208-
sql: 'SELECT * FROM test'
207+
action: "exec",
208+
sql: (
209+
"DROP TABLE IF EXISTS test;\n"
210+
+ "CREATE TABLE test (id INTEGER, age INTEGER, name TEXT);\n"
211+
+ "INSERT INTO test VALUES ($id1, :age1, @name1);\n"
212+
+ "INSERT INTO test VALUES ($id2, :age2, @name2);\n"
213+
+ "INSERT INTO test VALUES ($id3, :age3, @name3);\n"
214+
+ "SELECT id FROM test;\n"
215+
+ "SELECT age,name FROM test;\n"
216+
),
217+
params: {
218+
"$id1": 1,
219+
":age1": 1,
220+
"@name1": "Ling",
221+
"$id2": 2,
222+
":age2": 18,
223+
"@name2": "Paul",
224+
"$id3": 3,
225+
":age3": 3,
226+
"@name3": "Marcus"
227+
}
209228
});
210229
};
211230
212231
worker.onerror = e => console.log("Worker error: ", e);
213232
worker.postMessage({
214233
id:1,
215-
action:'open',
234+
action:"open",
216235
buffer:buf, /*Optional. An ArrayBuffer representing an SQLite Database file*/
217236
});
218237
</script>

src/api.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
651651
*
652652
* This is a wrapper against
653653
* {@link Database.prepare},
654+
* {@link Statement.bind},
654655
* {@link Statement.step},
655656
* {@link Statement.get},
656657
* and {@link Statement.free}.
@@ -660,7 +661,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
660661
* by a semicolon)
661662
*
662663
* ## Example use
663-
* We have the following table, named *test* :
664+
* We will create following table, named *test* and query it with a
665+
* multi-line statement using params:
664666
*
665667
* | id | age | name |
666668
* |:--:|:---:|:------:|
@@ -671,18 +673,44 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
671673
* We query it like that:
672674
* ```javascript
673675
* var db = new SQL.Database();
674-
* var res = db.exec("SELECT id FROM test; SELECT age,name FROM test;");
676+
* var res = db.exec(
677+
* (
678+
* "DROP TABLE IF EXISTS test;\n"
679+
* + "CREATE TABLE test (id INTEGER, age INTEGER, name TEXT);\n"
680+
* + "INSERT INTO test VALUES ($id1, :age1, @name1);\n"
681+
* + "INSERT INTO test VALUES ($id2, :age2, @name2);\n"
682+
* + "INSERT INTO test VALUES ($id3, :age3, @name3);\n"
683+
* + "SELECT id FROM test;\n"
684+
* + "SELECT age,name FROM test;\n"
685+
* ),
686+
* {
687+
* "$id1": 1,
688+
* ":age1": 1,
689+
* "@name1": "Ling",
690+
* "$id2": 2,
691+
* ":age2": 18,
692+
* "@name2": "Paul",
693+
* "$id3": 3,
694+
* ":age3": 3,
695+
* "@name3": "Marcus"
696+
* }
697+
* );
675698
* ```
676699
*
677700
* `res` is now :
678701
* ```javascript
679702
* [
680-
* {columns: ['id'], values:[[1],[2],[3]]},
681-
* {columns: ['age','name'], values:[[1,'Ling'],[18,'Paul'],[3,'Markus']]}
703+
* {"columns":["id"],"values":[[1],[2],[3]]},
704+
* {"columns":["age","name"],"values":[[1,"Ling"],[18,"Paul"],[3,"Marcus"]]}
682705
* ]
683706
* ```
684707
*
685-
* @param {string} sql a string containing some SQL text to execute
708+
@param {string} sql a string containing some SQL text to execute
709+
@param {any[]} [params=[]] When the SQL statement contains placeholders,
710+
you can pass them in here. They will be bound to the statement
711+
before it is executed. If you use the params argument as an array,
712+
you **cannot** provide an sql string that contains several queries
713+
(separated by `;`). This limitation does not apply to params as an object.
686714
* @return {Database.QueryExecResult[]} The results of each statement
687715
*/
688716
Database.prototype["exec"] = function exec(sql, params) {

0 commit comments

Comments
 (0)