多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ### web ``` <meta charset="utf8" /> <html> <script src='/dist/sql-wasm.js'></script> <script> config = { locateFile: filename => `/dist/${filename}` } // The `initSqlJs` function is globally provided by all of the main dist files if loaded in the browser. // We must specify this locateFile function if we are loading a wasm file from anywhere other than the current html page's folder. initSqlJs(config).then(function(SQL){ //Create the database const db = new SQL.Database(); // Run a query without reading the results db.run("CREATE TABLE test (col1, col2);"); // Insert two rows: (1,111) and (2,222) db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,222]); // Prepare a statement const stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end"); stmt.getAsObject({$start:1, $end:1}); // {col1:1, col2:111} // Bind new values stmt.bind({$start:1, $end:2}); while(stmt.step()) { // const row = stmt.getAsObject(); console.log('Here is a row: ' + JSON.stringify(row)); } }); </script> <body> Output is in Javascript console </body> </html> ``` ## web worker ``` <script> const worker = new Worker("/dist/worker.sql-wasm.js"); worker.onmessage = () => { console.log("Database opened"); worker.onmessage = event => { console.log(event.data); // The result of the query }; worker.postMessage({ id: 2, action: "exec", sql: "SELECT age,name FROM test WHERE id=$id", params: { "$id": 1 } }); }; worker.onerror = e => console.log("Worker error: ", e); worker.postMessage({ id:1, action:"open", buffer:buf, /*Optional. An ArrayBuffer representing an SQLite Database file*/ }); </script> ```