prepared statement에 하나 이상의 값이 바인딩되는 경우, 바인딩되는 변수의 값을 배열(array)로 전달받아 각각의 값을 변수에 바인딩하여 질의를 실행한다.
데이터를 바인딩하기 위해서는 cci_bind_param_array_size()함수를 호출하여 배열의 크기를 지정한 후, cci_bind_param_array() 함수를 이용하여 각각의 값을 변수에 바인딩하고, cci_execute_array()함수를 호출하여 질의를 실행한다.
cci_execute()함수를 호출하면 질의 수행 결과 셋을 가져올 수 있으나, cci_execute_array()함수는 query_result 변수로 수행된 질의 개수를 반환한다. 실행 결과에 대한 정보를 얻기 위해서는 아래와 같은 매크로를 이용할 수 있다. 매크로에서 입력받는 각 인자에 대한 유효성 검사가 이루어지지 않으므로 주의한다. query_result 변수의 사용이 끝나면 cci_query_result_free 함수를 이용하여 질의 결과를 삭제해야 한다.
매크로 |
리턴 값 타입 |
의미 |
---|---|---|
CCI_QUERY_RESULT_RESULT |
int |
결과 개수 |
CCI_QUERY_RESULT_ERR_MSG |
char* |
질의에 대한 에러 메시지 |
CCI_QUERY_RESULT_STMT_TYPE |
int (T_CCI_CUBRID_STMT enum) |
질의문의 타입 |
int cci_execute_array(int req_handle, T_CCI_QUERY_RESULT **query_result, T_CCI_ERROR *err_buf)
char *query =
"update participant set gold = ? where host_year = ? and nation_code = 'KOR'";
int gold[2];
char *host_year[2];
int null_ind[2];
T_CCI_QUERY_RESULT *result;
int n_executed;
...
req = cci_prepare (con, query, 0, &cci_error);
if (req < 0)
{
printf ("prepare error: %d, %s\n", cci_error.err_code,
cci_error.err_msg);
goto handle_error;
}
gold[0] = 20;
host_year[0] = "2004";
gold[1] = 15;
host_year[1] = "2008";
null_ind[0] = null_ind[1] = 0;
error = cci_bind_param_array_size (req, 2);
if (error < 0)
{
printf ("bind_param_array_size error: %d\n", error);
goto handle_error;
}
error =
cci_bind_param_array (req, 1, CCI_A_TYPE_INT, &gold, null_ind,
CCI_U_TYPE_INT);
if (error < 0)
{
printf ("bind_param_array error: %d\n", error);
goto handle_error;
}
error =
cci_bind_param_array (req, 2, CCI_A_TYPE_STR, &host_year, null_ind,
CCI_U_TYPE_INT);
if (error < 0)
{
printf ("bind_param_array error: %d\n", error);
goto handle_error;
}
n_executed = cci_execute_array (req, &result, &cci_error);
if (n_executed < 0)
{
printf ("execute error: %d, %s\n", cci_error.err_code,
cci_error.err_msg);
goto handle_error;
}
for (i = 1; i <= n_executed; i++)
{
printf ("query %d\n", i);
printf ("result count = %d\n", CCI_QUERY_RESULT_RESULT (result, i));
printf ("error message = %s\n", CCI_QUERY_RESULT_ERR_MSG (result, i));
printf ("statement type = %d\n",
CCI_QUERY_RESULT_STMT_TYPE (result, i));
}
error = cci_query_result_free (result, n_executed);
if (error < 0)
{
printf ("query_result_free: %d\n", error);
goto handle_error;
}