1

Problem

I need to access the data of an access database (*.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.

Approaches

I've found the 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:

[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
IJustDev
  • 101
  • 1
  • 9
  • Let me get this straight: you want to use Access from Node.JS, but magically without the use of any external drivers? Well, start [here](https://github.com/mdbtools/mdbtools/blob/dev/HACKING.md) I guess, and get to writing your own driver entirely in JavaScript. Good luck. – Erik A May 25 '21 at 08:08
  • @ErikA thanks a lot but this isn't the point basically. I just don't want the user to download anything manually. Including the driver in some sort of binary would work fine aswell. But this thing aside, what driver would I need to download in order for this to work? Jet.OLEDB.12? – IJustDev May 25 '21 at 08:11
  • 1
    Well, the driver is proprietary and conflicts with existing installs in a different bitness, so you're out of luck if you want to install the driver automatically. You really either want your user to properly setup Access, or want to use a different database altogether. – Erik A May 25 '21 at 08:14
  • 1
    See [Why am I getting “Data source name not found and no default driver specified” and how do I fix it?](https://stackoverflow.com/q/58571740/7296893) for the general case including a download link – Erik A May 25 '21 at 08:15
  • Thanks a lot @ErikA, really appreciate your help! – IJustDev May 25 '21 at 08:16

1 Answers1

2

I found another package called node-adodb. 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.

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))
});
IJustDev
  • 101
  • 1
  • 9