23.8.11.10 mysql_stmt_execute()
int mysql_stmt_execute(MYSQL_STMT *stmt)
설명
mysql_stmt_execute() 는 문 손잡이와 연관된 준비된 쿼리를 실행합니다. 현재 바인딩 된 매개 변수 마커 값이 호출 중에 서버에 전송되고 서버는 그 마커를이 새롭게 제공된 데이터로 바꿉니다.
mysql_stmt_execute() 의 뒤의 문 처리 명령문의 종류에 따라 다릅니다.
UPDATE,DELETE또는INSERT는 변경, 삭제 또는 삽입 된 행의 수를mysql_stmt_affected_rows()를 호출하여 찾을 수 있습니다.결과 세트를 생성하는
SELECT등의 문은 쿼리 처리가 발생하는 다른 모든 함수를 호출하기 전에mysql_stmt_fetch()를 호출하여 데이터를 가져올 필요가 있습니다. 결과를 가져 오는 방법에 대한 자세한 내용은 섹션 23.8.11.11 "mysql_stmt_fetch ()" 를 참조하십시오.mysql_stmt_execute()의 호출에 이어,mysql_store_result()또는mysql_use_result()를 호출하지 마십시오. 그 함수는 준비된 명령문에서 결과를 처리하는 것을 목적으로하지 않습니다.
결과 세트를 생성하는 명령문에 대해 mysql_stmt_execute() 명령문을 실행하기 전에 mysql_stmt_attr_set() 를 호출하여 문 커서를 열도록 요청할 수 있습니다. 문을 여러 번 실행하는 경우, mysql_stmt_execute() 은 새로운 커서를 열기 전에 열려있는 커서를 닫습니다.
준비된 문에서 참조되는 테이블이나 뷰의 메타 데이터 변경이 감지되고 그것이 다음 실행 때 문이 자동으로 다시 준비됩니다. 자세한 내용은 섹션 8.9.4 "준비된 문 및 저장 프로그램 캐시" 를 참조하십시오.
반환 값
성공의 경우는 제로. 오류가 발생한 경우 0이 아닌.
오류
CR_COMMANDS_OUT_OF_SYNC명령이 잘못된 순서로 실행되었습니다.
CR_OUT_OF_MEMORY메모리 부족.
CR_SERVER_GONE_ERRORMySQL 서버가 존재하지 않습니다.
CR_SERVER_LOST서버에 대한 연결이 쿼리 중에 손실되었습니다.
CR_UNKNOWN_ERROR알 수없는 오류가 발생했습니다.
Example
다음 예제에서는 mysql_stmt_init() , mysql_stmt_prepare() , mysql_stmt_param_count() , mysql_stmt_bind_param() , mysql_stmt_execute() 및 mysql_stmt_affected_rows() 을 사용하여 테이블을 만들고 채우는 방법을 보여줍니다. mysql 변수는 유효한 접속 핸들로 간주됩니다. 데이터를 가져 오는 방법을 보여주는 예제는 섹션 23.8.11.11 "mysql_stmt_fetch ()" 를 참조하십시오.
#define STRING_SIZE 50
#define DROP_SAMPLE_TABLE "DROP TABLE IF EXISTS test_table"
#define CREATE_SAMPLE_TABLE "CREATE TABLE test_table(col1 INT,\
col2 VARCHAR(40),\
col3 SMALLINT,\
col4 TIMESTAMP)"
#define INSERT_SAMPLE "INSERT INTO \
test_table(col1,col2,col3) \
VALUES(?,?,?)"
MYSQL_STMT *stmt;
MYSQL_BIND bind[3];
my_ulonglong affected_rows;
int param_count;
short small_data;
int int_data;
char str_data[STRING_SIZE];
unsigned long str_length;
my_bool is_null;
if (mysql_query(mysql, DROP_SAMPLE_TABLE))
{
fprintf(stderr, " DROP TABLE failed\n");
fprintf(stderr, " %s\n", mysql_error(mysql));
exit(0);
}
if (mysql_query(mysql, CREATE_SAMPLE_TABLE))
{
fprintf(stderr, " CREATE TABLE failed\n");
fprintf(stderr, " %s\n", mysql_error(mysql));
exit(0);
}
/* Prepare an INSERT query with 3 parameters */
/* (the TIMESTAMP column is not named; the server */
/* sets it to the current date and time) */
stmt = mysql_stmt_init(mysql);
if (!stmt)
{
fprintf(stderr, " mysql_stmt_init(), out of memory\n");
exit(0);
}
if (mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE)))
{
fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
fprintf(stdout, " prepare, INSERT successful\n");
/* Get the parameter count from the statement */
param_count= mysql_stmt_param_count(stmt);
fprintf(stdout, " total parameters in INSERT: %d\n", param_count);
if (param_count != 3) /* validate parameter count */
{
fprintf(stderr, " invalid parameter count returned by MySQL\n");
exit(0);
}
/* Bind the data for all 3 parameters */
memset(bind, 0, sizeof(bind));
/* INTEGER PARAM */
/* This is a number type, so there is no need
to specify buffer_length */
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[0].buffer= (char *)&int_data;
bind[0].is_null= 0;
bind[0].length= 0;
/* STRING PARAM */
bind[1].buffer_type= MYSQL_TYPE_STRING;
bind[1].buffer= (char *)str_data;
bind[1].buffer_length= STRING_SIZE;
bind[1].is_null= 0;
bind[1].length= &str_length;
/* SMALLINT PARAM */
bind[2].buffer_type= MYSQL_TYPE_SHORT;
bind[2].buffer= (char *)&small_data;
bind[2].is_null= &is_null;
bind[2].length= 0;
/* Bind the buffers */
if (mysql_stmt_bind_param(stmt, bind))
{
fprintf(stderr, " mysql_stmt_bind_param() failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Specify the data values for the first row */
int_data= 10; /* integer */
strncpy(str_data, "MySQL", STRING_SIZE); /* string */
str_length= strlen(str_data);
/* INSERT SMALLINT data as NULL */
is_null= 1;
/* Execute the INSERT statement - 1*/
if (mysql_stmt_execute(stmt))
{
fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Get the number of affected rows */
affected_rows= mysql_stmt_affected_rows(stmt);
fprintf(stdout, " total affected rows(insert 1): %lu\n",
(unsigned long) affected_rows);
if (affected_rows != 1) /* validate affected rows */
{
fprintf(stderr, " invalid affected rows by MySQL\n");
exit(0);
}
/* Specify data values for second row,
then re-execute the statement */
int_data= 1000;
strncpy(str_data, "
The most popular Open Source database",
STRING_SIZE);
str_length= strlen(str_data);
small_data= 1000; /* smallint */
is_null= 0; /* reset */
/* Execute the INSERT statement - 2*/
if (mysql_stmt_execute(stmt))
{
fprintf(stderr, " mysql_stmt_execute, 2 failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Get the total rows affected */
affected_rows= mysql_stmt_affected_rows(stmt);
fprintf(stdout, " total affected rows(insert 2): %lu\n",
(unsigned long) affected_rows);
if (affected_rows != 1) /* validate affected rows */
{
fprintf(stderr, " invalid affected rows by MySQL\n");
exit(0);
}
/* Close the statement */
if (mysql_stmt_close(stmt))
{
fprintf(stderr, " failed while closing the statement\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
준비된 문 함수의 사용 완전한 예제는 파일 tests/mysql_client_test.c 를 참조하십시오. 이 파일은 MySQL 소스 배포판 또는 Bazaar 소스 저장소에서 얻을 수 있습니다.