Skip to content

Commit cc53f69

Browse files
authored
Merge pull request #213 from kim366/master
Readme Enchancements
2 parents bc1d22f + 34714fc commit cc53f69

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

README.md

+46-45
Original file line numberDiff line numberDiff line change
@@ -74,38 +74,38 @@ The test files provide up to date example of the use of the api.
7474
```html
7575
<script src='js/sql.js'></script>
7676
<script>
77-
//Create the database
78-
var db = new SQL.Database();
79-
// Run a query without reading the results
80-
db.run("CREATE TABLE test (col1, col2);");
81-
// Insert two rows: (1,111) and (2,222)
82-
db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,222]);
83-
84-
// Prepare a statement
85-
var stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
86-
stmt.getAsObject({$start:1, $end:1}); // {col1:1, col2:111}
87-
88-
// Bind new values
89-
stmt.bind({$start:1, $end:2});
90-
while(stmt.step()) { //
91-
var row = stmt.getAsObject();
92-
// [...] do something with the row of result
93-
}
77+
//Create the database
78+
var db = new SQL.Database();
79+
// Run a query without reading the results
80+
db.run("CREATE TABLE test (col1, col2);");
81+
// Insert two rows: (1,111) and (2,222)
82+
db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,222]);
83+
84+
// Prepare a statement
85+
var stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
86+
stmt.getAsObject({$start:1, $end:1}); // {col1:1, col2:111}
87+
88+
// Bind new values
89+
stmt.bind({$start:1, $end:2});
90+
while(stmt.step()) { //
91+
var row = stmt.getAsObject();
92+
// [...] do something with the row of result
93+
}
9494
</script>
9595
```
9696

9797
#### Creating a database from a file choosen by the user
9898
`SQL.Database` constructor takes an array of integer representing a database file as an optional parameter.
9999
The following code uses an HTML input as the source for loading a database:
100100
```javascript
101-
dbFileElm.onchange = function() {
102-
var f = dbFileElm.files[0];
103-
var r = new FileReader();
104-
r.onload = function() {
105-
var Uints = new Uint8Array(r.result);
106-
db = new SQL.Database(Uints);
107-
}
108-
r.readAsArrayBuffer(f);
101+
dbFileElm.onchange = () => {
102+
var f = dbFileElm.files[0];
103+
var r = new FileReader();
104+
r.onload = function() {
105+
var Uints = new Uint8Array(r.result);
106+
db = new SQL.Database(Uints);
107+
}
108+
r.readAsArrayBuffer(f);
109109
}
110110
```
111111
See : http://kripken.github.io/sql.js/GUI/gui.js
@@ -117,7 +117,7 @@ var xhr = new XMLHttpRequest();
117117
xhr.open('GET', '/path/to/database.sqlite', true);
118118
xhr.responseType = 'arraybuffer';
119119

120-
xhr.onload = function(e) {
120+
xhr.onload = e => {
121121
var uInt8Array = new Uint8Array(this.response);
122122
var db = new SQL.Database(uInt8Array);
123123
var contents = db.exec("SELECT * FROM my_table");
@@ -164,25 +164,26 @@ You will need to download `worker.sql.js`
164164
Example:
165165
```html
166166
<script>
167-
var worker = new Worker("js/worker.sql.js"); // You can find worker.sql.js in this repo
168-
worker.onmessage = function() {
169-
console.log("Database opened");
170-
worker.onmessage = function(event){
171-
console.log(event.data); // The result of the query
172-
};
173-
worker.postMessage({
174-
id: 2,
175-
action: 'exec',
176-
sql: 'SELECT * FROM test'
177-
});
178-
};
179-
180-
worker.onerror = function(e) {console.log("Worker error: ", e)};
181-
worker.postMessage({
182-
id:1,
183-
action:'open',
184-
buffer:buf, /*Optional. An ArrayBuffer representing an SQLite Database file*/
185-
});
167+
var worker = new Worker("js/worker.sql.js"); // You can find worker.sql.js in this repo
168+
worker.onmessage = () => {
169+
console.log("Database opened");
170+
worker.onmessage = event => {
171+
console.log(event.data); // The result of the query
172+
};
173+
174+
worker.postMessage({
175+
id: 2,
176+
action: 'exec',
177+
sql: 'SELECT * FROM test'
178+
});
179+
};
180+
181+
worker.onerror = e => console.log("Worker error: ", e);
182+
worker.postMessage({
183+
id:1,
184+
action:'open',
185+
buffer:buf, /*Optional. An ArrayBuffer representing an SQLite Database file*/
186+
});
186187
</script>
187188
```
188189

0 commit comments

Comments
 (0)