Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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.

share|improve this question
1  
If you are only interested in the database aspect itself, I would suggest IndexedDB. –  Overv Mar 14 '13 at 16:55
    
SQL.js is usable in plain Javascript as well. However, it does require Node.js as a Javascript runtime. –  robertklep Mar 14 '13 at 17:02
    
but, once I have the plain javascript, do I have to embedded it in the html like this: <script src="sql.js" type="text/javascript"></script>?? Because it gives me the error Module not defined –  synack Mar 15 '13 at 11:28

2 Answers 2

up vote 4 down vote accepted

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>
share|improve this answer
1  
Error ! There is no such column a. did u mean a as a column name ? –  rashidnk Jul 30 at 4:50

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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.