我想将javascript脚本中的数据直接存储在SQLite数据库中。我找到了这个SQL.js库,它是javascript的一个端口。然而,显然它只适用于coffeescript。有人知道如何在javascript中使用它吗?关于如何在SQLite DB中存储数据的其他想法也很受欢迎。
发布于 2014-05-27 01:09:04
更新
sql.js现在有了自己的github组织,我和原作者都是该组织的成员:https://github.com/sql-js/sql.js/。
API本身现在是用javascript编写的。
原始答案
我是sqlite to javascript最新版本的移植的作者:https://github.com/lovasoa/sql.js
它基于您提到的那个(https://github.com/kripken/sql.js),但包括许多改进,包括完整的文档:http://lovasoa.github.io/sql.js/documentation/
以下是如何使用此版本的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>发布于 2014-01-03 23:56:17
我在纯JavaScript中使用SQL.js,没有任何问题。只需包含以下文件:
https://stackoverflow.com/questions/15415076
复制相似问题