目前,我正在从事一个项目,该项目要求后端在 oracle
中完成。我使用了给定的 link并安装了 node-oracledb在我的 Mac 上使用 npm。我的文件内容如下
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);
});
}
});
当我运行 node filename.js 时出现以下错误
ORA-12154: TNS:could not resolve the connect identifier specified
我使用的 Node 版本是 v7.0.0
,npm 版本是 v3.10.8
。我的 oracle 数据库也是云上的 11g
实例。有人可以让我知道我做错了什么吗?
最佳答案
根据 Docs,您的 connectString 看起来是错误的它只是主机名:端口/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:监听器当前不知道连接描述符中请求的服务
相反,发现 here您可以将 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)))"
})
关于javascript - 使用 Node oracle-db npm 包时出现 ORA-12514 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41681688/