使用OCI获取/设置TIMESTAMP值

时间:2018-07-14 09:57:35

标签: oracle oci

我正在使用C和oci.h来设置和获取VARCHAR,NUMBER,... DATE值,但我还需要TIMESTAMP类型。我可以从OCIAttrGet的每一列上读取大小,比例和精度

获取

在我的OCIDefineByPos中,我如何知道要为TIMESTAMP类型分配多少空间? 当我OCIStmtFetch2()时,如何解释检索到的值?

设置

在OCIBindByPos()和OCIStmtExecute()中,如何将时间戳格式转换为oracle的格式? 我也需要知道所需的空间。

1 个答案:

答案 0 :(得分:1)

帮助获取时间戳(来自https://docs.oracle.com/html/E49886_05/oci12oty.htm

示例12-2操纵OCIDateTime类型的属性

...

/* allocate the program variable for storing the data */
OCIDateTime *tstmpltz = (OCIDateTime *)NULL;

/* Col1 is a time stamp with local time zone column */
OraText *sqlstmt = (OraText *)"SELECT col1 FROM foo";

/* Allocate the descriptor (storage) for the data type */
status = OCIDescriptorAlloc(envhp,(void  **)&tstmpltz, OCI_DTYPE_TIMESTAMP_LTZ,
         0, (void  **)0);
....

status = OCIStmtPrepare (stmthp, errhp, sqlstmt, (ub4)strlen ((char *)sqlstmt),
         (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT);

/* specify the define buffer for col1 */
status = OCIDefineByPos(stmthp, &defnp, errhp, 1, &tstmpltz, sizeof(tstmpltz),
         SQLT_TIMESTAMP_LTZ, 0, 0, 0, OCI_DEFAULT);

/* Execute and Fetch */
OCIStmtExecute(svchp, stmthp, errhp, 1, 0,(OCISnapshot *) NULL,
         (OCISnapshot *)NULL, OCI_DEFAULT)

At this point tstmpltz contains a valid time stamp with local time zone data. You
can get the time zone name of the datetime data using:

status = OCIDateTimeGetTimeZoneName(envhp, errhp, tstmpltz, (ub1 *)buf,
         (ub4 *)&buflen);
...