如何使用节点js同步读取sqlite3数据库? [英] How to read a sqlite3 database, using node js, synchronously?

查看:174
本文介绍了如何使用节点js同步读取sqlite3数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

exports.allProbes = function() {
    var rows = db.all("SELECT * FROM probes;");
    return rows;
};


main:
var json_values = allProbes();

是否可以做类似的事情? 我的意思是,不使用回调函数:只是从数据库读取数据(同步模式).并返回json格式的输出?

Is it possible to do something like that? I mean, without using a callback function: just, read data (sync mode) from the db. and return a json formatted output?

谢谢.

推荐答案

您将无法使用sqlite3做到这一点.使用sqlite3模块时,唯一可用的操作模式是异步执行,您将不得不使用回调.例如

You will not be able to do that with sqlite3. With sqlite3 module the only available mode of operation is asynchronous execution, and you will have to use a callback. Eg.

exports.allProbes = function(callback) {
    db.all("SELECT * FROM probes;", function(err, all) {
        callback(err, all);  
    });
};

然后在您的代码中:

var json_values;

allProbes(function(err, all) {
    json_values = all;
});

检查 sqlite3 API文档.

这篇关于如何使用节点js同步读取sqlite3数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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