使用 OCILIB 连接数据库

Database connection using OCILIB

大家好, 我是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;
}

它工作正常,但我希望这个程序的输出到一个包机数组中,而不是下面的行

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

我试过了, 测试[1]=OCI_GetString(rs, 2);

但它给我错误

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

有人请帮助我。

您不能使用 = # 字符串 分配给 char[] 数组。你必须这样做

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

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

#初始化时间除外