Skip to content

Commit f59bcf7

Browse files
committed
update demo with pull request sql-js#3 with sample sqlite databases
1 parent 4e7d90e commit f59bcf7

File tree

6 files changed

+28
-20
lines changed

6 files changed

+28
-20
lines changed

package-lock.json

+4-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"devDependencies": {
1818
"copy-webpack-plugin": "^5.1.1",
19-
"react-app-rewired": "^2.1.5",
19+
"react-app-rewired": "^2.1.8",
2020
"typescript": "3.3.3"
2121
},
2222
"scripts": {

public/test.db

12 KB
Binary file not shown.

public/z_test.db

12 KB
Binary file not shown.

src/App.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,30 @@ export default class App extends React.Component {
1414
// sql.js needs to fetch its wasm file, so we cannot immediately instantiate the database
1515
// without any configuration, initSqlJs will fetch the wasm files directly from the same path as the js
1616
// see ../config-overrides.js
17-
initSqlJs()
18-
.then(SQL => this.setState({ db: new SQL.Database() }))
19-
.catch(err => this.setState({ err }));
17+
const me = this;
18+
Promise.all([initSqlJs(), fetch('./z_test.db')]).then(async res => {
19+
const SQLite = res[0], dbStorage = res[1];
20+
const db = new SQLite.Database(new Uint8Array(await dbStorage.arrayBuffer()));
21+
22+
me.setState({db: db});
23+
}).catch(err => {
24+
me.setState({err});
25+
});
2026
}
2127

2228
exec(sql) {
2329
let results = null, err = null;
2430
try {
25-
// The sql is executed synchronously on the UI thread.
31+
// The sql is executed synchronously on the UI thread.
2632
// You may want to use a web worker
2733
results = this.state.db.exec(sql); // an array of objects is returned
2834
} catch (e) {
2935
// exec throws an error when the SQL statement is invalid
3036
err = e
3137
}
3238
this.setState({ results, err })
39+
console.log('results')
40+
console.log(results)
3341
}
3442

3543
/**
@@ -70,7 +78,8 @@ export default class App extends React.Component {
7078
<textarea
7179
onChange={e => this.exec(e.target.value)}
7280
placeholder="Enter some SQL. No inspiration ? Try “select sqlite_version()”"
73-
></textarea>
81+
defaultValue="SELECT * FROM table1"
82+
/>
7483

7584
<pre className="error">{(err || "").toString()}</pre>
7685

z_test.sql

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.open C:/path/to/test.db;
2+
3+
create table table1(
4+
var1 varchar(10),
5+
var2 smallint);
6+
7+
insert into table1 values ('A', 1);
8+
insert into table1 values ('B', 2);
9+

0 commit comments

Comments
 (0)