SQL.js in javascript

I want to store data in a SQLite database directly from a javascript script. I found this SQL.js library , which is the port for javascript. However, it appears to be available only for coffeescript. Does anyone know how to use it in javascript? Other ideas on how to store data in SQLite DB are also welcome.

+5
source share
2 answers

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

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

Here is an example of using this version 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>
+9
source

I am using SQL.js from pure JavaScript without any problems. Just add the following file:

https://cdn.rawgit.com/kripken/sql.js/master/js/sql.js

+1
source

All Articles