NOTE: CRT is excited to have Mark Lesswing, Chief Technology Officer and SVP of ITS at NAR, write a series of posts on his experiences with node.js. In the article below, Mark covers the use of SQLite and node.js. Please note that this will be a pretty technical series and speaks to the diversity of topics CRT will cover. We will indicate when we have articles that are more technical as we post them. Thank you. – Chad

In a previous post, I discussed the concept of using node.js as a WebSocket server. This approach is characterized by many smaller node.js servers, each providing information to separate WebSockets. The user experiences the data through an HTML5 webpage. It is natural that the next topic of discussion would be persistence. There is a wealth of information that can be found on the web regarding node.js and robust SQL databases like MySQL, so I will not duplicate the information in this post.

In keeping with the “smallness” of specificity of purpose of the approach, I will now discuss the use of SQLite in conjunction with node.js. The source code for SQLite is in the public domain, so it is a good mate for node.js.

The first thing you need to do is install the node.js module for SQLite. If you have already installed node.js which included the npm tool (I covered this in a previous post) you need to execute the following from the command line in the root of you project directory:

The node-gyp module is a dependency of the sqlite module.

Now let’s look at a quick code example:

The each function is self-explanatory, so I would like to talk about the all function. Let’s consider the case where you need to determine of any results came back. Look at this code fragment:

Another common use case is issuing SQL calls that depend on previous SQL results; in other words, the order of SQl calls is important. If you have been experimenting with node.js, you will appreciate this next sequence. The event-oriented nature of node.js takes some getting used to but here is how to use serialize():

The serialize() call ensures that the underlying SQLite database executes the AQL in the proper order.

Hopefully this persistence discussion was helpful.

 

3 Responses to Geeking Out: SQLite and Node.js

  1. Kenson Goo says:

    There is no data sanitization in UPDATE SQL queries, which might lead to SQL injection. Other than that, this is an awesome tutorial.

    Kenson Goo
    Contra Costa Assocation of Realtors

  2. mlesswing says:

    I try to be careful about entering user contributed data into the database. I use SQLite for non-consumer facing storage.

  3. mlesswing says:

    A better answer would be:

    var firstValue = "hello";
    var secondValue = 4;
    db.prepare("INSERT INTO myExample (hostName,hitCount) VALUES(?,?)").run(firstValue,secondValue).finalize();

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">



Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can
take care of it!

Visit our friends!

A few highly recommended friends...