目前,我正在做一个需要后端在oracle
完成的项目。我使用了给定的链接,并在Mac上使用npm安装了node-oracledb。我的文件内容如下
var oracledb = require('oracledb');
oracledb.getConnection(
{
user : 'username',
password : 'password',
connectString : 'username/password//hostname:port/sid'
function(err, connection)
{
if (err) {
console.error(err.message);
return;
}else{
connection.execute(
"SELECT * from TableName",
function(err, result)
{
if (err) { console.error(err); return; }
console.log(result.rows);
});
}
});
当我运行节点文件名时.js出现以下错误
ORA-12154: TNS:could not resolve the connect identifier specified
我正在使用节点版本是v7.0.0
和npm版本是v3.10.8
。此外,我的预言机数据库是云上的11g
实例。有人可以让我知道我做错了什么吗?
看起来你的connectString是错误的,根据Docs,它只是主机名:port/sid
var oracledb = require('oracledb');
oracledb.getConnection(
{
user : "hr",
password : "welcome",
connectString : "hostname:port/sid"
})
.then(function(conn) {
return conn.execute(
"SELECT department_id, department_name " +
"FROM departments " +
"WHERE manager_id < :id",
[110] // bind value for :id
)
.then(function(result) {
console.log(result.rows);
return conn.close();
})
.catch(function(err) {
console.error(err);
return conn.close();
});
})
.catch(function(err) {
console.error(err);
});
编辑:
至少从 2019 年 7 月开始(可能在 7 月之前的某个时间),connectString : "hostname:port/sid"
不再使用 oracledb
打印错误: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
相反,如此处找到的,您可以将connectString
设置为 (DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = YOUR_HOST)(PORT = YOUR_PORT))(CONNECT_DATA =(SID= YOUR_SID)))
使用 SID 连接到数据库。
因此,更新getConnection
(关于该问题)将是:
oracledb.getConnection(
{
user : "hr",
password : "welcome",
connectString : "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = port))(CONNECT_DATA =(SID= sid)))"
})