如何使用 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 应用,安装 hana client 开发库:

npm install --save @sap/hana-client

完整的源代码:

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 代码里打印的数据库表 REGIONS,来自 PLAIN schema:

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

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

此时,Node.js 应用报错:

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

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章