What's the best way to read Sqlite3 directly in Browser using Javascript?

后端 未结 2 1838
小鲜肉
小鲜肉 2020-12-28 14:32

For one of our Insights platform, we plan to generate summary SQLite3 databases in the background and let it be rendered on the browser as charts. Currently, we are intendin

2条回答
  •  清酒与你
    2020-12-28 15:27

    There is a javascript library called sql.js that can do exactly what you want. In your case, you would use it like that

    const SQL = await initSqlJs(options);
    const fetched = await fetch("/path/to/database.sqlite");
    const buf = await fetched.arrayBuffer();
    const db = new SQL.Database(new Uint8Array(buf));
    const contents = db.exec("SELECT * FROM my_table");
    // contents is now [{columns:['col1','col2',...], values:[[first row], [second row], ...]}]
    

    See the documentation on sql-js.github.io/sql.js/documentation/

提交回复
热议问题