Connecting to Oracle with olog() at specific hostname:port

穿精又带淫゛_ 提交于 2019-12-25 01:54:08

问题


The C-program I'm writing tries to connect to Oracle database with olog():

olog(&lda, (ub1 *)hda, dbparam_keys[DbUser].value, -1,
        dbparam_keys[DbPassword].value, -1, NULL, -1, (ub4)OCI_LM_DEF);

This fails with ORA-12545, because I specify neither the server's hostname, nor the port number anywhere. I do not, because I do not know, how to do that...

The "demos" bundled with the client seem to presume a valid tnsnames.ora -- is having that file mandatory for OCI API, or a can a client program, that knows the necessary values, specify them to the library (such as with OCIAttrSet()) all by itself?


回答1:


There, likely, exists a better way, but the following works. The connection-specifier -- the third string passed to olog() -- normally refers to an entry found in tnsnames.ora. But, instead of being such a shortcut, it can also be the entire entry, which is what I want in this case.

So, I'm using the complete syntax normally used in tnsnames.ora to describe the server in my code. I then pass the text as the connection-specifier to olog():

bzero(&lda, sizeof(lda));
bzero(&hda, sizeof(hda));

connlen = snprintf(buf, sizeof buf,
    "(DESCRIPTION =\n"
    "   (ADDRESS_LIST =\n"
    "       (ADDRESS =\n"
    "           (PROTOCOL = TCP)\n"
    "           (HOST = %s)\n"
    "           (PORT = %s)\n"
    "       )\n"
    "   )\n"
    "   (CONNECT_DATA =\n"
    "       (SID = %s)\n"
    "   )\n"
    ")",
    dbparam_keys[DbHostName].value,
    dbparam_keys[DbServerPortNumber].value,
    dbparam_keys[DbServerOrServiceName].value);

if (connlen >= sizeof buf)
    errx(EX_SOFTWARE, "Internal error: buffer not big enough: "
        "need %zd, have %zd bytes", connlen, sizeof buf);
if (verbose)
    warnx("Connecting to %.*s", connlen, buf);

if (olog(&lda, (ub1 *)hda,
    dbparam_keys[DbUser].value, -1,
    dbparam_keys[DbPassword].value, -1,
    buf, connlen,
    OCI_LM_DEF)) {
    errx(EX_NOPERM, "Logging into Oracle failed: %s",
        oraerr(&lda, buf, sizeof(buf)));
}


来源:https://stackoverflow.com/questions/48715018/connecting-to-oracle-with-olog-at-specific-hostnameport

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!