0

我正在使用wasm sqlite构建前端应用程序我有以下运行良好的代码:

const SQLpath = '/www/wasm/sql';
const sqlPromise = initSqlJs({
    locateFile: file => `${SQLpath}/sql-wasm.wasm`
  });
const dataPromise = fetch("http://localhost:8080/www/data/database.sqlite").then(res => res.arrayBuffer());
sqlRun();
async function sqlRun(){
  const [SQL, buf] = await Promise.all([sqlPromise, dataPromise])

  const db = new SQL.Database(new Uint8Array(buf));

  // 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));
  }

  // Downloading the database.sqlite
  const data = db.export();
 // const buffer = new Buffer(data);
 // fs.writeFileSync("filename.sqlite", buffer);
  const a = document.createElement('a');
  const blob = new Blob([data]);
  a.href = URL.createObjectURL(blob);
  a.download = 'database.sqlite';                     //filename to download
  a.click();
}

最后一个块是downloading最新的副本,database.sqlite但我需要更新原始后端文件,有没有办法可以更新位于的原始文件http://localhost:8080/www/data/database.sqlite

我的应用程序结构是:

在此处输入图像描述

4

0 回答 0