JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript – 如何在节点中的MDB文件中读取双精度类型的列?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用 MDBTools,unixodbcnode odbc包在linux上的nodejs中查询一些MDB文件.

使用此代码

db.query("SELEct my_str_col,my_dbl_col from my_table",function (err,rows) {
    if (err) return console.log(err);
    console.log(rows);
    db.close();
});

我可以查询my_str_col字符串列,但我无法解密my_dbl_col Double列,我得到这样的结果:

[ { my_str_col: 'bla',my_dbl_col: '{\u0014�Gai�@' },{ my_str_col: 'bla bla',my_dbl_col: '' },{ my_str_col: 'bla',my_dbl_col: '�G�z\u0014NF@' } ]

所有非空字符串都是7或8字节,但最困扰我的是这个例子的第二行,我得到一个空字符串,而我知道MDB中有一个非空数字:这意味着我无法尝试构建字符串字节中的数字.

那么,如何在linux上的节点中读取MDB文件中的Double类型的数字?

我确切地说

>像MDBViewer这样的工具(使用MDBTools)正确读取这些数字
> JavaScript编号对我来说足够精确:这些数字都适合float32
>我无法在MDB文件上应用冗长的转换:我必须对几百个经常更改的文件进行快速查询…
>我不能真正发出查询但是让我阅读整个表格的解决方案也是可以接受的

解决方法

由于我无法获得node-odbc来正确解读数字,我写了一个函数调用 mdb-export(这是非常快)并读取整个表.
var fs   = require("fs"),spawn  = require('child_process').spawn,byline = require('byline'); // npm install byline   

// Streaming reading of choosen columns in a table in a MDB file. 
// parameters :
//   args :
//     path : mdb file complete path
//     table : name of the table
//     columns : names of the desired columns
//   read : a callBACk accepTing a row (an array of Strings)
//   done : an optional callBACk called when everything is finished with an error code or 0 as argument
function querymdbFile(args,read,donE) {
    var cmd = spawn('/usr/bin/mdb-export',[args.path,args.table]);
    var rowIndex = 0,colIndexes;
    byline(cmd.stdout).on('data',function (linE) {
        var cells = line.toString().split(',');
        if (!rowIndex++) { // first line,let's find the col indexes
            var lc = function(s){ return s.toLowerCase() };
            colIndexes = args.columns.map(lC).map(function(Name) {
                return cells.map(lC).indexOf(Name);
            });
        } else { // other lines,let's give to the callBACk the required cells
            read(colIndexes.map(function(indeX){ return ~index ? cells[index] : null }));
        }
    });
    cmd.on('exit',function (codE) {
        if (donE) done(codE);
    });
}

这是一个示例,其中我构建了一个包含问题示例的所有行的数组:

var rows = [];
querymdbFile({
    path: "mydatabase.MDB",table: 'my_table',columns : ['my_str_col','my_dbl_col']
},function(row) {
    rows.push(row);
},function(errorCodE) {
    console.log(errorCode ? ('error:'+errorCodE) : 'done');
});

一切都被读作字符串,但很容易解析:

[ ['bla','1324'  ],['bla bla','332e+5'],['bla','43138' ] ]

令人惊讶的是,这比使用Node-odbc和linuxodbc查询要快.

大佬总结

以上是大佬教程为你收集整理的javascript – 如何在节点中的MDB文件中读取双精度类型的列?全部内容,希望文章能够帮你解决javascript – 如何在节点中的MDB文件中读取双精度类型的列?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。