使用OCILIB进行数据库连接

时间:2015-03-11 06:31:44

标签: c oci

大家好, 我是C语言的新手, 我使用OCILIB连接了数据库,我在网上找到了这个程序。

#include "ocilib.h"
#include <conio.h>

/* Example on Microsoft platform */

static HANDLE evt;

void long_oracle_call(void *data)
{
 OCI_Statement *st  = OCI_StatementCreate((OCI_Connection *) data); 
OCI_Resultset *rs;

/* execute a query that takes a long time to process */

OCI_ExecuteStmt(st, "select * from employees2");

rs = OCI_GetResultset(st);

while (OCI_FetchNext(rs))
{
    printf("%i - %s", OCI_GetInt(rs, 1), OCI_GetString(rs, 2));
}

SetEvent(evt);
}


int main(void)
{
 OCI_Connection *cn;

if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))
    return EXIT_FAILURE;

    cn = OCI_ConnectionCreate("localhost:1522/infodba", "system", "infodba",      OCI_SESSION_DEFAULT);
 evt = CreateEvent(0, TRUE, FALSE, 0);

 _beginthread(long_oracle_call, 0, cn);

  if (WaitForSingleObject(evt, 10000) != WAIT_OBJECT_0)
  {
    OCI_Break(cn);
    Sleep(2000);
  }

  OCI_Cleanup();
  getch();

  return EXIT_SUCCESS;
}

IT工作正常,但我想将此程序的输出转换为包机数组,而不是在行

之下
printf("%i - %s", OCI_GetInt(rs, 1), OCI_GetString(rs, 2));

我试着这样做, test [1] = OCI_GetString(rs,2);

但它给我的错误是

ERROR: A value of type "const text*" cannot be assigned to an entity of type   "char".

有人请帮助我。

1 个答案:

答案 0 :(得分:0)

您无法使用char[] 字符串分配给= arrary。你应该这样做

strcpy(test, OCI_GetString(rs, 2));

假设text是一个足够长的char数组。

#初始化时间