简体   繁体   English

从 NodeJs 读取访问数据库

[英]Read Access Database from NodeJs

Problem问题

I need to access the data of an access database (*.mdb).我需要访问访问数据库 (*.mdb) 的数据。 The approach should be written in nodejs and be used by multiple users that should NOT be required to make any changes to the ODBC drivers list in Windows.该方法应该用nodejs编写,并由多个用户使用,不需要对 Windows 中的ODBC驱动程序列表进行任何更改。

Approaches方法

I've found the node-odbc .我找到了node-odbc Looking at the documentation following snippet should work just fine:查看以下代码片段的文档应该可以正常工作:

const db = require('odbc');

const cn = `Provider=Microsoft.Jet.OLEDB.12.0;Data Source=C:\\Users\\some\\Dev\\db.mdb`;

db.connect(cn, (err, connection) => {
    if (err) {
        console.error(err);
    }
    console.log(connection);
});

Unfortunately is this the output I get:不幸的是,这是我得到的 output:

[Error: [odbc] Error connecting to the database] {
  odbcErrors: [
    {
      state: 'IM002',
      code: 0,
      message: '[Microsoft][ODBC Driver Manager] The DSN could not be found and' +
        'there was no default driver specified'
    }
  ]
}
null

I found another package called node-adodb .我发现了另一个名为node-adodb package 。 The following snippet works just fine for me with a password protected database.下面的代码片段对我来说非常适合使用受密码保护的数据库。 Furthremore fixes this package the difference between x64 and x86 by using the SysWOW64 cscripts.exe file.此外,使用 SysWOW64 cscripts.exe文件修复了此 package x64 和 x86 之间的差异。

const adodb = require('node-adodb');

const cn = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb;Jet OLEDB:Database Password=MyPassword;';

const connection = adodb.open(cn);

connection.query("SELECT name FROM users WHERE firstname='John';").then((data) => {
    console.log(data);
}).catch(err => {
    console.error(JSON.stringify(err))
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM