Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL.js in javascript

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.

like image 483
synack Avatar asked Mar 14 '13 16:03

synack


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>
like image 51
lovasoa Avatar answered Nov 14 '22 06:11

lovasoa


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

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

like image 42
Dan Halliday Avatar answered Nov 14 '22 07:11

Dan Halliday