2013-08-01 3 views
5

MDBTools, unixodbcnode odbc 패키지를 사용하여 Linux에서 nodejs의 일부 MDB 파일을 쿼리하고 있습니다.노드의 MDB 파일에서 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\[email protected]' } ] 

모든 비어 있지 않은 문자열이 있습니다 : 나는 이런 식으로 뭔가를 얻을,이 코드

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_colDouble 열을 해독 할 수 있습니다 7 또는 8 바이트하지만 가장 신경 쓰이는 것은 MDB에 null이 아닌 숫자가 있다는 것을 알고있는 동안 빈 문자열을 얻는이 예제의 두 번째 행입니다. 즉, 문자열 바이트에서 숫자를 만들려고 할 수 없다는 뜻입니다 .

그렇다면 Linux의 노드에있는 MDB 파일에서 Double 유형의 번호를 읽으려면 어떻게해야합니까?

  • 이 MDBViewer 같은 도구가 (MDBTools 사용) 정확하게 그 숫자 읽고

    I 정확한 것을

  • 자바 스크립트 번호는 나에게 충분히 정확합니다 : 그 숫자는 모두 float32
  • 에 딱 맞는 나는 '수 MDB 파일에 대한 긴 변환 적용 : 자주 변경되는 수백 개의 파일에 대한 빠른 쿼리를 작성해야합니다 ...
  • 쿼리를 실행할 수 없지만 전체 테이블을 읽을 수있는 솔루션도 허용됩니다

답변

3

node-odbc에서 숫자를 올바르게 해독 할 수 없기 때문에 mdb-export (매우 빠름)을 호출하고 전체 테이블을 읽는 함수를 작성했습니다.

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' ] ] 

놀랍게도을 여기

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); 
    }); 
} 

내가 질문의 예를 들어 모든 행 배열을 구축하는 예입니다 이것은 node-odbc 및 linuxodbc를 사용하여 쿼리하는 것보다 빠릅니다.

+0

Samba를 사용하여 마운트 된 파일에이 기능을 적용하려는 사용자는 다음 관련 질문에 관심을 가질 수 있습니다. http://askubuntu.com/questions/339354/cant-alloc-filename-when-executing-mdb-export-on-a -mounted-file/339602? noredirect = 1 # comment433332_339602 –

관련 문제