C 언어 Mysql 함수 조작

8002 단어

데이터 유형

  • MYSQL
    MYSQL , , 
    
    
     typedef struct st_mysql
     {
       NET           net;                    /* Communication parameters */
       unsigned char *connector_fd;          /* ConnectorFd for SSL */
       char          *host,*user,*passwd,*unix_socket,*server_version,*host_info;
       char          *info, *db;
       struct charset_info_st *charset;
       MYSQL_FIELD   *fields;
       MEM_ROOT      field_alloc;
       my_ulonglong affected_rows;
       my_ulonglong insert_id;               /* id if insert on table with NEXTNR */
       my_ulonglong extra_info;              /* Not used */
       unsigned long thread_id;              /* Id for connection in server */
       unsigned long packet_length;
       unsigned int  port;
       unsigned long client_flag,server_capabilities;
       unsigned int  protocol_version;
       unsigned int  field_count;
       unsigned int  server_status;
       unsigned int  server_language;
       unsigned int  warning_count;
       struct st_mysql_options options;
       enum mysql_status status;
       my_bool       free_me;                /* If free in mysql_close */
       my_bool       reconnect;              /* set to 1 if automatic reconnect */
    
       /* session-wide random string */
       char          scramble[SCRAMBLE_LENGTH+1];
    
     /*
       Set if this is the original connection, not a master or a slave we have
       added though mysql_rpl_probe() or mysql_set_master()/ mysql_add_slave()
     */
       my_bool rpl_pivot;
       /*
         Pointers to the master, and the next slave connections, points to
         itself if lone connection.
       */
       struct st_mysql* master, *next_slave;
    
       struct st_mysql* last_used_slave; /* needed for round-robin slave pick */
     /* needed for send/read/store/use result to work correctly with replication */
       struct st_mysql* last_used_con;
    
       LIST  *stmts;                     /* list of all statements */
       const struct st_mysql_methods *methods;
       void *thd;
       /*
         Points to boolean flag in MYSQL_RES  or MYSQL_STMT. We set this flag
         from mysql_stmt_close if close had to cancel result set of this object.
       */
       my_bool *unbuffered_fetch_owner;
       /* needed for embedded server - no net buffer to store the 'info' */
       char *info_buffer;
       void *extension;
     } MYSQL;
  • MYSQL_RES MYSQL_RES 구조는 반환 행의 검색 결과(SELECT, SHOW, DESCRIBE 등)를 나타내며 데이터베이스에서 데이터를 읽고 마지막으로 MYSQL_RES에서 데이터를 읽습니다.MYSQL_RES는 다음과 같이 정의합니다typedef struct st_mysql_res { my_ulonglong row_count; MYSQL_FIELD *fields; MYSQL_DATA *data; MYSQL_ROWS *data_cursor; unsigned long *lengths; /* column lengths of current row */ MYSQL *handle; /* for unbuffered reads */ const struct st_mysql_methods *methods; MYSQL_ROW row; /* If unbuffered read */ MYSQL_ROW current_row; /* buffer to current row */ MEM_ROOT field_alloc; unsigned int field_count, current_field; my_bool eof; /* Used by mysql_fetch_row */ /* mysql_stmt_close() had to cancel this result */ my_bool unbuffered_fetch_cancelled; void *extension; } MYSQL_RES;

  • 데이터베이스 함수


    1 mysql_real_connect


    함수 원형은 다음과 같습니다.
    MYSQL * STDCALL mysql_real_connect(MYSQL *mysql, const char *host,
    const char *user,
    const char *passwd,
    const char *db,
    unsigned int port,
    const char *unix_socket,
    unsigned long clientflag);

    예:
    #include   
    #include   
        
    int main(int argc, const char *argv[])  
    {  
        MYSQL   mysql;  
        
        if (NULL == mysql_init(&mysql)) {    // MYSQL   
            printf("mysql_init(): %s
    ", mysql_error(&mysql)); return -1; } // MySQL if (NULL == mysql_real_connect(&mysql, "localhost", "root", "shallnet", "db_users", 0, NULL, 0)) { printf("mysql_real_connect(): %s
    ", mysql_error(&mysql)); return -1; } printf("Connected MySQL successful!
    "); mysql_close(&mysql); return 0; }

    2 데이터 조회 문장 함수 mysql_query


    함수 원형
    int STDCALL mysql_query(MYSQL *mysql, const char *q);

    질의 예:
    #include 
    #include 
    #include 
    int main(int argc, const char *argv[])
    {
        MYSQL           mysql;
        MYSQL_RES       *res = NULL;
        MYSQL_ROW       row;
        char            *query_str = NULL;
        int             rc, i, fields;
        int             rows;
        if (NULL == mysql_init(&mysql)) {
            printf("mysql_init(): %s
    ", mysql_error(&mysql)); return -1; } if (NULL == mysql_real_connect(&mysql, "localhost", "root", "shallnet", "db_users", 0, NULL, 0)) { printf("mysql_real_connect(): %s
    ", mysql_error(&mysql)); return -1; } printf("1. Connected MySQL successful!
    "); query_str = "select * from tb_users"; rc = mysql_real_query(&mysql, query_str, strlen(query_str)); if (0 != rc) { printf("mysql_real_query(): %s
    ", mysql_error(&mysql)); return -1; } res = mysql_store_result(&mysql); if (NULL == res) { printf("mysql_restore_result(): %s
    ", mysql_error(&mysql)); return -1; } rows = mysql_num_rows(res); printf("The total rows is: %d
    ", rows); fields = mysql_num_fields(res); printf("The total fields is: %d
    ", fields); while ((row = mysql_fetch_row(res))) { for (i = 0; i < fields; i++) { printf("%s\t", row[i]); } printf("
    "); } mysql_close(&mysql); return 0; }

    삭제 예를 삽입하려면 다음과 같이 하십시오.
    #include 
    #include 
    #include 
    int main(int argc, const char *argv[])
    {
        MYSQL           mysql;
        MYSQL_RES       *res = NULL;
        MYSQL_ROW       row;
        char            *query_str = NULL;
        int             rc, i, fields;
        int             rows;
        if (NULL == mysql_init(&mysql)) {
            printf("mysql_init(): %s
    ", mysql_error(&mysql)); return -1; } if (NULL == mysql_real_connect(&mysql, "localhost", "root", "shallnet", "db_users", 0, NULL, 0)) { printf("mysql_real_connect(): %s
    ", mysql_error(&mysql)); return -1; } printf("1. Connected MySQL successful!
    "); query_str = "select * from tb_users"; rc = mysql_real_query(&mysql, query_str, strlen(query_str)); if (0 != rc) { printf("mysql_real_query(): %s
    ", mysql_error(&mysql)); return -1; } res = mysql_store_result(&mysql); if (NULL == res) { printf("mysql_restore_result(): %s
    ", mysql_error(&mysql)); return -1; } rows = mysql_num_rows(res); printf("The total rows is: %d
    ", rows); fields = mysql_num_fields(res); printf("The total fields is: %d
    ", fields); while ((row = mysql_fetch_row(res))) { for (i = 0; i < fields; i++) { printf("%s\t", row[i]); } printf("
    "); } mysql_close(&mysql); return 0; }
    gcc :gcc ***.c -o *** -lmysqlclient

    참고


    linux에서 C 언어 프로그래밍 작업 MySQL 데이터베이스
    다음으로 전송:https://www.cnblogs.com/daibigmonster/p/8603936.html

    좋은 웹페이지 즐겨찾기