0
点赞
收藏
分享

微信扫一扫

如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据


登录 SAP Business Technology Platform,找到 space 下自己创建好的 HANA Cloud 实例,右键菜单选择 Copy SQL Endpoint,将 HANA Cloud 实例的 endpoint 拷贝下来:

25eeba68-24ce-4b2d-aaa5-ee8d599ff4a0.hana.trial-eu10.hanacloud.ondemand.com:443

稍后我们在 Node.js 代码里,会使用到。

如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据_node.js

如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据_node.js_02

新建一个 Node.js 应用,安装 hana client 开发库:

npm install --save @sap/hana-client

如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据_数据库表_03

完整的源代码:

var hana = require("@sap/hana-client");

var connOptions = {
serverNode: "25eeba68-24ce-4b2d-aaa5-ee8d599ff4a0.hana.trial-eu10.hanacloud.ondemand.com:443",
encrypt: "true",
sslValidateCertificate: "false",
uid: "DBADMIN",
pwd: "Sgd",
};

var dbConnection = hana.createConnection();

dbConnection.connect(connOptions, function (err) {
if (err) throw err;
dbConnection.exec(
`SELECT TOP 1000
"REGION",
"DESCRIPTION"
FROM "PLAIN"."REGIONS"`,
function (err, result) {
if (err) throw err;
for( var i = 0; i < result.length; i++)
console.log(result[i]);
dbConnection.disconnect();
}
);
});

执行结果:

如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据_node.js_04

Node.js 代码里打印的数据库表 REGIONS,来自 PLAIN schema:

如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据_big data_05

注意,使用 DBADMIN 这个用户,只能访问到该用户拥有 access right 的数据库表。

例如我们更换使用 Node.js 访问的数据库表名称为 DB1.COMMUNITY:

如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据_node.js_06

此时,Node.js 应用报错:


Error: insufficient privilege: Detailed info for this error can be found with guid ‘6F6F68ED5F58804CB8C7B25D7559D0E0’


如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据_node.js_07

更多Jerry的原创文章,尽在:“汪子熙”:

如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据_node.js_08



举报

相关推荐

0 条评论