I want to store data in a SQLite database directly from a javascript script. I found this SQL.js library 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.
2 Answers
Update
sql.js now has its own github organisation, where both the original author and I are members: https://github.com/sql-js/sql.js/ .
The API itself itself is now written in javascript.
Original answer
I am the author of this port of the latest version of sqlite to 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/
Here is an example of how to use this version of 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>
-
2
-
2The code in this repository is out of date. This fork has finally been merged into kripken’s original repo. Please use that repository instead of this one.– XatenevMar 17, 2017 at 9:08
I'm using SQL.js from pure JavaScript without any problems. Simply include the following file:
<script src="sql.js" type="text/javascript"></script>
?? Because it gives me the errorModule not defined