🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[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> ```