아래의 예제는 CCI를 이용하여 DB에 접속하고 SQL 질의를 수행시켜 결과를 출력하는 프로그램이다. 이 예제는 CCI를 이용하여 SQL 질의를 수행하는 전형적인 과정을 보여준다. 먼저 cci_connect() 함수를 이용하여 DB에 접속한 후, cci_prepare() 함수를 호출하여 SQL을 준비한다. 다음으로 cci_execute() 함수를 호출하여 SQL을 수행하게 한 후, cci_cursor(), cci_fetch(), cci_get_data() 함수를 이용하여 질의 수행 결과를 가져온다. 마지막으로 cci_close_conn_handle(), cci_disconnect() 함수를 호출하여 DB와의 접속을 해제한다.
#include
<stdio.h>
#include
<stdarg.h>
#include
"cas_cci.h"
int
dump_all_rows(int req);
int
print_result(const char *format, ...);
void
show_error(char *msg, int code, T_CCI_ERROR *error);
int main()
{
char *cas_ip = "localhost";
int cas_port = 30000;
char *dbname = "subway";
int con =
0, req = 0, res;
char *sql;
T_CCI_ERROR
error;
sql = "select * from line";
con = cci_connect(cas_ip, cas_port, dbname, "PUBLIC",
"");
if
(con < 0) {
fprintf(stderr, "%s(%d): ", __FILE__, __LINE__);
show_error("cci_connect error(%d)", con, NULL);
goto handle_error;
}
req = cci_prepare(con, sql, 0, &error);
if (req < 0) {
fprintf(stderr, "%s(%d): ", __FILE__, __LINE__);
show_error("cci_prepare error(%d)", req, &error);
goto handle_error;
}
res = cci_execute(req, 0, 0, &error);
if (res < 0) {
fprintf(stderr, "%s(%d): ", __FILE__, __LINE__);
show_error("cci_execute error(%d)", res,
&error);
goto handle_error;
}
res = cci_fetch_size(req, 200);
if (res < 0) {
fprintf(stderr, "%s(%d): ", __FILE__, __LINE__);
show_error("cci_fetch_size error(%d)", res,
NULL);
goto handle_error;
}
res = dump_all_rows(req);
if (res < 0) {
goto handle_error;
}
res = cci_close_conn_handle(req);
if (res < 0) {
fprintf(stderr, "%s(%d): ", __FILE__, __LINE__);
show_error("cci_close_conn_handle error(%d)", res,
NULL);
goto handle_error;
}
res = cci_disconnect(con, &error);
if (res < 0) {
fprintf(stderr, "%s(%d): ", __FILE__, __LINE__);
show_error("cci_disconnect error(%d)", res,
&error);
goto handle_error;
}
return 0;
handle_error:
if (req > 0)
cci_close_conn_handle(req);
if (con > 0)
cci_disconnect(con, &error);
return -1;
}
int
dump_all_rows(int req)
{
T_CCI_ERROR error;
T_CCI_COL_INFO *column_info;
T_CCI_SQLX_CMD cmd_type;
char
*buffer;
int
column_count, ind, res, i;
column_info = cci_get_result_info(req, &cmd_type, &column_count);
if (!column_info) {
show_error("get_result_info error", 0, NULL);
return -1;
}
print_result("Dumping all rows of the result(%d)\n", req);
res = cci_cursor(req, 1, CCI_CURSOR_FIRST, &error);
if (res == CCI_ER_NO_MORE_DATA) {
print_result("Done!\n");
return 0;
}
if (res < 0) {
fprintf(stderr, "%s(%d): ", __FILE__, __LINE__);
show_error("cci_cursor error(%d)", res, &error);
goto handle_error;
}
while (1) {
res = cci_fetch(req, &error);
if (res < 0) {
fprintf(stderr, "%s(%d): ", __FILE__, __LINE__);
show_error("cci_fetch error(%d)", res, &error);
goto handle_error;
}
for (i=0 ; i < column_count ; i++) {
res = cci_get_data(req, i+1, CCI_A_TYPE_STR, &buffer, &ind);
if (res < 0) {
fprintf(stderr, "%s(%d): ", __FILE__, __LINE__);
show_error("cci_get_data error(%d)", res, NULL);
goto handle_error;
}
if (ind < 0) {
print_result("(NULL)|");
} else {
print_result("%s|", buffer);
}
}
print_result("\n");
res = cci_cursor(req, 1, CCI_CURSOR_CURRENT, &error);
if (res == CCI_ER_NO_MORE_DATA) {
print_result("Done!\n");
break;
}
if (res < 0) {
fprintf(stderr, "%s(%d): ", __FILE__, __LINE__);
show_error("cci_cursor error(%d)", res, &error);
goto handle_error;
}
}
return 0;
handle_error:
return -1;
}
void
show_error(char *msg, int code, T_CCI_ERROR *error)
{
fprintf(stderr, msg, code);
fprintf(stderr, "\n");
if (code == CCI_ER_DBMS) {
fprintf(stderr, "\tDBMS Error:(%d) %s\n",
error->err_code,
error->err_msg);
}
}
int
print_result(const char *format, ...)
{
va_list
args;
int res;
va_start
(args, format);
res =
vprintf(format, args);
va_end (args);
return res;
}