9

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.

3
  • 1
    If you are only interested in the database aspect itself, I would suggest IndexedDB. Commented Mar 14, 2013 at 16:55
  • 2
    SQL.js is usable in plain Javascript as well. However, it does require Node.js as a Javascript runtime. Commented Mar 14, 2013 at 17:02
  • 1
    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 Commented Mar 15, 2013 at 11:28

2 Answers 2

16

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Error ! There is no such column a. did u mean a as a column name ?
The 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.
I was recommended to write something out using sql.js from ChatGPT for a small project, and unfortunately it seems the syntax of the library is quite different today vs some time ago. It took me a long time to find a version of the file (0.5.0) that worked for the code samples I found online, including this one.
2

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.