开发者

SQLite on nodejs for Windows

I have a dead-simple question. How to use any SQLite library with nodejs under Windows? I have looked into the sqlite3 lib and it requires compilation which is linux-specific. In general, this is not the first time I'm facing a question of this type. Same problem I had with TameJS. If anyone knows any solution for this, I would be grateful. Google is silent.

PS: Cygwin is not an option. The target application should be an easy in usage portable database, driven by nodejs, and having a web-browser int开发者_JAVA技巧erface.


Read at the bottom for Update 2 (2013-12-06) - there is now a pure JS module which looks promising for windows work...

I've been having a similar problem, and shy some pretty magic with re-linking the windows SQLite (or compiling the existing nodejs packages) the best I could come up with is to use a child process and shell out to the console sqlite client and then capture the results... Not the prettiest, and I have no idea if this can scale, but in a limited intranet scenario it might be Good Enough™.

Update:

Here's some code which I cobbled together - give you an idea in case you're not that familiar with child_process module. This works on nodejs 0.6.0.

First I build up the command required to pull something out of sqlite:

var cmd = '"' + sqlitePath + '" "' + dbPath + '" "select * from sqlite_master"';

Note, if you include the optional flags -noheader and -list then you might get slightly better output for parsing. You can also control the field delimiter by using -separator 'x' where x is your delimiter.

Then I actually spawn the child process:

var child = exec(cmd, function(error, stdout, stderr) {
        if (error) {
            console.log('error while trying to do stuff...');
            // error message in error and stderr
        } else {
            console.log('success');
            // successful stuff is in stdout
        }
    });

In combination with some string parsing and playing with the sqlite console client's options you may be able to get this to do enough. I do say again, however, this is not production tested - I'm just mocking up some intranet stuff, and even so I'm sure it's not the best approach!

Update 2 (2013-12-06):

For folks reading this question now, you might be interested to check out sql.js on github; it is a pure JS implementation of sqlite which looks promising for interacting on windows (or whatever platform). Please note, to install via npm the package name is node-sqlite-purejs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜