• MySQL매뉴얼
    • MySQL 5.6 매뉴얼
    • MySQL 5.1 매뉴얼
    • MySQL 5.0 매뉴얼
    • MySQL HA 매뉴얼
  • 기술문서
    • Xtrabackup 구성
    • 메모리 사용량 모니터링
  • 라이선스
  • 온라인문의
  • 회사소개
  • → 목 록 (MySQL5.6 한글메뉴얼) [close]
  • 1. MySQL 5.6 새로운 기능
  • 2. MySQL 설치 및 업그레이드
  • 3. MySQL Tutorial
  • 4. MySQL 프로그램
  • 5. MySQL 서버관리
  • 6. 보안
  • 7. 백업 및 복구
  • 8. 최적화
  • 9. Language Structure(언어구조)
  • 10. Character Sets(Globalization)
  • 11. 데이터형(Data Types)
  • 12. 함수와 연산자
  • 13. SQL 문법
  • 14. InnoDB 스토리지 엔진
  • 15. 기타 스토리지 엔진
  • 16. 고가용성 및 확장성
  • 17. 리플리케이션
  • 18. MySQL Cluster
  • 19. 파티셔닝
  • 20. Stored Programs and Views
  • 21. INFORMATION_SCHEMA
  • 22. PERFORMANCE SCHEMA
  • 23. 컨넥터 및 API
  • 1. MySQL Connector / ODBC
    2. MySQL Connector/Net
    3. MySQL Connector/J
    4. MySQL Connector/C++
    5. MySQL Connector/C
    6. MySQL Connector/Python
    7. libmysqld, the Embedded MySQL Server Library
    8. MySQL C API
    1. MySQL C API 구현
    2. MySQL Server MySQL Connector/C 동시 설치
    3. C API 클라이언트 프로그램 예제
    4. Building and Running C API Client Programs
    5. C API Data 구조
    6. C API Function 개요
    7. C API Function 설명
    8. C API Prepared Statements
    9. C API Prepared Statement Data 구조
    10. C API Prepared Statement 함수 개요
    11. C API Prepared Statement 함수 설명
    1. mysql_stmt_affected_rows()
    2. mysql_stmt_attr_get()
    3. mysql_stmt_attr_set()
    4. mysql_stmt_bind_param()
    5. mysql_stmt_bind_result()
    6. mysql_stmt_close()
    7. mysql_stmt_data_seek()
    8. mysql_stmt_errno()
    9. mysql_stmt_error()
    10. mysql_stmt_execute()
    11. mysql_stmt_fetch()
    12. mysql_stmt_fetch_column()
    13. mysql_stmt_field_count()
    14. mysql_stmt_free_result()
    15. mysql_stmt_init()
    16. mysql_stmt_insert_id()
    17. mysql_stmt_next_result()
    18. mysql_stmt_num_rows()
    19. mysql_stmt_param_count()
    20. mysql_stmt_param_metadata()
    21. mysql_stmt_prepare()
    22. mysql_stmt_reset()
    23. mysql_stmt_result_metadata()
    24. mysql_stmt_row_seek()
    25. mysql_stmt_row_tell()
    26. mysql_stmt_send_long_data()
    27. mysql_stmt_sqlstate()
    28. mysql_stmt_store_result()
    12. C API Threaded 함수 설명
    13. C API 임베디드 서버 함수 설명
    14. C API Client Plugin 함수
    15. C API를 사용하는 경우 일반적인 질문과 문제
    16. 자동 재 연결 동작 제어
    17. 여러 명령문 실행 C API 지원
    18. C API Prepared Statement 문제
    19. C API Prepared Statement date 및 time 값 처리
    20. C API의 Prepared CALL Statements 지원
    9. MySQL PHP API
    10. MySQL Perl API
    11. MySQL Python API
    12. MySQL Ruby APIs
    13. MySQL Tcl API
    14. MySQL Eiffel Wrapper
  • 24. MySQL 확장
  • 25. MySQL Enterprise Edition
  • 26. MySQL Workbench
  • 27. 제약 및 제한
  • 28. MySQL 5.7 새로운 기능

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_ERROR

    MySQL 서버가 존재하지 않습니다.

  • 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 소스 저장소에서 얻을 수 있습니다.


서울시 강남구 영동대로 602 6층
TEL: 02-6061-0006  /  E: csr@mysqlkorea.com
주식회사 이노클러스터  등록번호 : 727-86-02261
Copyright © innocluster Co. ltd. all rights reserved