[英]Using a Microsoft access DB .accdb on a linux server with Node.js
So, I got this working on windows easy, and that is not surprising.所以,我很容易就能在 windows 上工作,这并不奇怪。 But I am more concerned about running this on a production linux server.
但我更关心在生产 linux 服务器上运行它。
Is there a way to get the Access Runtime 2013 to work on a linux based system without throwing some weird way of doing it.有没有一种方法可以让 Access Runtime 2013 在基于 linux 的系统上运行,而不用采用一些奇怪的方法。 If not, then I am going to have to rethink my plans.
如果没有,那么我将不得不重新考虑我的计划。
Just as a code base this is what I am doing:就像代码库一样,这就是我正在做的:
const ADODB = require('node-adodb');
ADODB.debug = true;
//Bring in LUT Database
const LUTDB = ADODB.open(
'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.accdb;Persist Security Info=False'
);
LUTDB.query('SELECT * FROM Table1')
.then(data => {
console.log(data);
})
.catch(err => {
console.log(err);
});
Like @ErikA suggests in his answer, if anyone is using Linux or MacOS, you can use BOTH:就像@ErikA 在他的回答中建议的那样,如果有人使用 Linux 或 MacOS,您可以同时使用:
I'm using MacOS Catalina.我正在使用 MacOS Catalina。 You can initialize a project with npm in a new directory:
您可以在新目录中使用 npm 初始化项目:
npm init
npm i --save jdbc
mkdir index.js
Download and copy the 5 UCanAccess jar files:下载并复制5个UCanAccess jar文件:
ucanaccess-5.0.1.jar
ucanaccess-5.0.1.jar
lib
directory lib
目录下的所有 4 个 jar 文件They can be put in the same directory too.它们也可以放在同一个目录中。 I created a simple MS Access
.accdb
file with 1 table: employees (id, name, photo)
我用 1 个表创建了一个简单的 MS Access
.accdb
文件: employees (id, name, photo)
And, in index.js
file, here is the codes to connect and SELECT to DB (most codes are from JDBC package, with a few modifications marked with // CUSTOM
over the modified line):并且,在
index.js
文件中,这里是连接和 SELECT 到 DB 的代码(大部分代码来自 JDBC package,在修改行上标有// CUSTOM
的一些修改):
var JDBC = require('jdbc');
var jinst = require('jdbc/lib/jinst');
if (!jinst.isJvmCreated()) {
jinst.addOption("-Xrs");
// CUSTOM
jinst.setupClasspath([
'./drivers/ucanaccess-5.0.1.jar',
'./drivers/commons-lang3-3.8.1.jar',
'./drivers/commons-logging-1.2.jar',
'./drivers/hsqldb-2.5.0.jar',
'./drivers/jackcess-3.0.1.jar'
]);
}
var config = {
// Required
// CUSTOM
url: 'jdbc:ucanaccess:///Users/Keitel/repos/mjsp/badge/badge-test.accdb',
// Optional
// CUSTOM
drivername: 'net.ucanaccess.jdbc.UcanaccessDriver',
minpoolsize: 10,
maxpoolsize: 100,
// Note that if you sepecify the user and password as below, they get
// converted to properties and submitted to getConnection that way. That
// means that if your driver doesn't support the 'user' and 'password'
// properties this will not work. You will have to supply the appropriate
// values in the properties object instead.
// user: 'SA',
// CUSTOM
user: '',
password: '',
properties: {}
};
var hsqldb = new JDBC(config);
hsqldb.initialize(function(err) {
if (err) {
console.log(err);
// CUSTOM
return;
}
// CUSTOM
console.log('successfully initialized');
});
// This assumes initialization as above.
// For series execution.
var asyncjs = require('async');
hsqldb.reserve(function(err, connObj) {
// The connection returned from the pool is an object with two fields
// {uuid: <uuid>, conn: <Connection>}
if (connObj) {
console.log("Using connection: " + connObj.uuid);
// Grab the Connection for use.
var conn = connObj.conn;
// Adjust some connection options. See connection.js for a full set of
// supported methods.
asyncjs.series([
function(callback) {
conn.setAutoCommit(false, function(err) {
if (err) {
callback(err);
} else {
callback(null);
}
});
},
// CUSTOM
// Skip Schema selection with MS Access DB
], function(err, results) {
// Check for errors if need be.
// results is an array.
if (err) {
console.log('autocommit error', err);
}
});
// Query the database.
asyncjs.series([
function(callback) {
// Select statement example.
conn.createStatement(function(err, statement) {
if (err) {
callback(err);
} else {
// Adjust some statement options before use. See statement.js for
// a full listing of supported options.
statement.setFetchSize(100, function(err) {
if (err) {
callback(err);
} else {
// CUSTOM
statement.executeQuery(
'SELECT id, name FROM employees;',
function(err, resultset) {
if (err) {
callback(err)
} else {
resultset.toObjArray(function(err, results) {
console.log('results', results);
callback(null, resultset);
});
}
}
);
}
});
}
});
},
], function(err, results) {
// Results can also be processed here.
// Release the connection back to the pool.
hsqldb.release(connObj, function(err) {
if (err) {
console.log(err.message);
}
});
});
}
});
The results came out in terminal as:结果在终端中显示为:
results [
{ ID: 1, name: 'Keitel Jovin' },
{ ID: 2, name: 'Jetro Joseph' },
{ ID: 3, name: 'Bertha Bazile' }
]
Nope. 不。
The Access Runtime (as well as the database engine and full application) is only compatible with Windows, and barring Wine stuff/other trickery that's not production-worthy, it plain won't work. Access Runtime(以及数据库引擎和完整的应用程序)仅与Windows兼容,除非存在不符合生产价值的Wine东西/其他欺骗手段,否则,它将无法正常工作。
There are third-party ODBC/JDBC drivers for Access out there that do work on Linux, but nothing official from Microsoft. 那里有用于Access的第三方ODBC / JDBC驱动程序,它们可以在Linux上运行,但是Microsoft没有官方提供的驱动程序。
My recommendation would be: either don't use Access on Linux (best), or try UCanAccess with a JDBC wrapper (not affiliated). 我的建议是:要么不要在Linux上使用Access(最好),要么尝试使用带有JDBC包装程序的UCanAccess(不附属)。 Note that the accdb file format has changed a number of times, so you can expect trouble/incompatibilities if you don't use an actively maintained product.
请注意,accdb文件格式已更改了许多次,因此,如果您不使用主动维护的产品,可能会遇到麻烦/不兼容的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.