JavaScript 中的 SQL.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15415076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:42:38  来源:igfitidea点击:

SQL.js in javascript

javascriptsqlite

提问by synack

I want to store data in a SQLite database directly from a javascript script. I found this SQL.jslibrary that is a port for javascript. However, apparently it's only available for coffeescript. Does anyone know how to use it in javascript? Other ideas about how to store data in SQLite DB are welcomed too.

我想直接从 javascript 脚本将数据存储在 SQLite 数据库中。我发现这个SQL.js库是 javascript 的一个端口。但是,显然它仅适用于 coffeescript。有谁知道如何在javascript中使用它?关于如何在 SQLite DB 中存储数据的其他想法也受到欢迎。

回答by lovasoa

I am the author of this port of the latest version of sqlite to javascript: https://github.com/lovasoa/sql.js

我是最新版本的sqlite到javascript的这个端口的作者:https: //github.com/lovasoa/sql.js

It is based on the one you mentioned (https://github.com/kripken/sql.js), but includes many improvements, including a full documentation: http://lovasoa.github.io/sql.js/documentation/

它基于您提到的那个 ( https://github.com/kripken/sql.js),但包括许多改进,包括完整的文档:http: //lovasoa.github.io/sql.js/documentation/

Here is an example of how to use this version of sql.js

以下是如何使用此版本的示例 sql.js

<script src='js/sql.js'></script>
<script>
    //Create the database
    var 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
    var 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()) { //
        var row = stmt.getAsObject();
        // [...] do something with the row of result
    }
</script>

回答by Dan Halliday

I'm using SQL.js from pure JavaScript without any problems. Simply include the following file:

我正在使用纯 JavaScript 中的 SQL.js,没有任何问题。只需包含以下文件:

https://cdnjs.com/libraries/sql.js

https://cdnjs.com/libraries/sql.js