SQLite3 Node.js JSON [英] SQLite3 Node.js JSON

查看:87
本文介绍了SQLite3 Node.js JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sqlite3 NPM软件包.我想将JSON存储在我的数据库列之一中.我了解SQLite本身可以存储JSON https://www.sqlite.org/json1.html,但我不确定如何通过Node.js做到这一点.

I am using the sqlite3 NPM package. I would like store JSON in one of my database columns. I understand that SQLite itself is able to store JSON https://www.sqlite.org/json1.html, but I am not necessarily sure how I would do this through Node.js.

在使用sqlite3 NPM包存储JSON之前,是否有人遇到过这种情况?使用轻量级的NoSQL数据库会更好吗?

Has anybody ran into this scenario before, using the sqlite3 NPM package to store JSON? Would I be better off using a lightweight NoSQL database?

推荐答案

默认情况下,sqlite3软件包支持Sqlite JSON1扩展.略微介绍一下sqlite3软件包提供的示例:

The sqlite3 package supports the Sqlite JSON1 extension by default. Riffing slightly on the example provided by the sqlite3 package:

const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database(':memory:');

db.serialize(function() {
    db.run('CREATE TABLE lorem (info TEXT)');
    let stmt = db.prepare('INSERT INTO lorem VALUES(json(?))');
    for (let i=0; i<10; i++) {
        stmt.run(JSON.stringify({ a: i }));
    }
    stmt.finalize();

    db.each('SELECT rowid AS id, json_extract(info, \'$.a\') AS info FROM lorem', function(err, row) {
        console.log(row.id + ": " + row.info);
    });
});

请注意,对于某些软件包配置或安装,默认情况下可能不包括JSON1扩展名.如果这不适合您,请参阅Mike Hardy对此答案的评论.

Note that for some package configurations or installations, the JSON1 extension may not be included by default. If this is not working for you, see the comment on this answer from Mike Hardy.

这篇关于SQLite3 Node.js JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆