登录
首页 >  数据库 >  MySQL

C 扩展库 - mysql API CRUD

来源:SegmentFault

时间:2023-01-10 14:57:27 261浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习数据库相关编程知识。下面本篇文章就来带大家聊聊《C 扩展库 - mysql API CRUD》,介绍一下MySQL、C,希望对大家的知识积累有所帮助,助力实战开发!

CRUD

table

create table if not exists `student` (
    `id` int auto_increment,
    `name` varchar(16) not null,
    `age` int not null,
    `address` varchar(128) not null,
    primary key (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

code

//
// Created by zhangrongxiang on 2018/3/5 14:56
// File main
//

#include 
#include 
#include 
#include 

int main() {
    MYSQL *mysql = NULL;
//    const char *host = "debian";
    const char *host = "localhost";
    const char *user = "root";
    const char *passwd = "root";
    const char *db = "fgap_config";
    unsigned int port = 3306;
    const char *unix_socket = "/var/run/mysqld/mysqld.sock";
    int flag = 0, i = 0;
    const char *db2 = "test";
    char sql[1024] = {0};

    if (mysql_library_init(0, NULL, NULL)) {
        fprintf(stderr, "could not initialize MySQL client library\n");
        exit(EXIT_FAILURE);
    }
    mysql = mysql_init(mysql);
    if (mysql == NULL) {
        perror("mysql_init error");
        return EXIT_FAILURE;
    }
    printf("mysql_init success\n");
    mysql = mysql_real_connect(mysql, host, user, passwd, db, port, unix_socket, 0);
    if (mysql == NULL) {
        fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(mysql));
        return EXIT_FAILURE;
    }
    printf("mysql_real_connect success\n");
    int ping = mysql_ping(mysql);
    if (ping == 0) {
        printf("mysql is active !\n");
    } else {
        return EXIT_FAILURE;
    }

    //////////////////////////////////////////////////////////////////
    const char *clientInfo = mysql_get_client_info();
    //5.5.58
    printf("mysql_get_client_info : %s\n", clientInfo);

    unsigned long version = mysql_get_client_version();
    //50558 = 5*10000 + 5*100 + 58
    //major_version*10000 + release_level*100 + sub_version
    printf("mysql_get_client_version : %ld\n", version);

    const char *serverInfo = mysql_get_server_info(mysql);
    //5.5.58-0+deb8u1
    printf("mysql_get_server_info : %s\n", serverInfo);

    const char *hostInfo = mysql_get_host_info(mysql);
    //Localhost via UNIX socket
    printf("mysql_get_host_info : %s\n", hostInfo);

    unsigned int protoInfo = mysql_get_proto_info(mysql);
    //10
    printf("mysql_get_proto_info : %d\n", protoInfo);

    unsigned long serverVersion = mysql_get_server_version(mysql);
    //50558
    printf("mysql_get_server_version : %ld\n", serverVersion);

    const char *mysqlInfo = mysql_info(mysql);
    //(null)
    printf("mysql_info : %s\n", mysqlInfo);

    const char *stat = mysql_stat(mysql);
    //mysql_stat : Uptime: 70217  Threads: 2  Questions: 824  Slow queries: 0  Opens: 69  Flush tables: 1  Open tables: 59  Queries per second avg: 0.011
    printf("mysql_stat : %s\n", stat);
    //////////////////////////////////////////////////////////////////

    MYSQL_RES *pRes = mysql_list_dbs(mysql, NULL);
    MYSQL_ROW row = NULL;
    if (pRes) {
        unsigned int fields = mysql_num_fields(pRes);
        my_ulonglong rows = mysql_num_rows(pRes);
        unsigned int count = mysql_field_count(mysql);
        printf("mysql_num_fields : %d\n", fields);//1
        printf("mysql_num_rows : %ld\n", (long) rows);//4
        printf("mysql_field_count : %d\n", count);//1
        while ((row = mysql_fetch_row(pRes)) != NULL) { // 打印结果集
            // database: information_schema
            // database: fgap_config
            // database: mysql
            // database: performance_schema
            if (strcmp(row[fields - 1], db2) == 0) {
                flag = 1;
            }
            printf("database: %-10s\n", row[fields - 1]);
        }
        mysql_free_result(pRes);
    }

    if (flag == 0) {
        printf("no %s db -> %d\n", db2, flag);
        //int mysql_create_db(MYSQL *mysql, const char *db)
        /*
         * //main.c:(.text+0x34f): undefined reference to `mysql_create_db'
        if(mysql_create_db(mysql,"test") == 0){
            printf("create test success\n");
        } else{
            printf("create test error\n");
        }
         */
    }
    if (mysql_select_db(mysql, db2) == 0) {
        printf("mysql_select_db(mysql, %s) success \n", db2);
        MYSQL_RES *tables = mysql_list_tables(mysql, NULL);
        unsigned int fields = mysql_num_fields(tables);
        my_ulonglong rows = mysql_num_rows(tables);
        printf("mysql_num_fields : %d\n", fields);//1
        printf("mysql_num_rows : %d\n", (int) rows);//1
        if (tables) {
            flag = 0;
            MYSQL_ROW fetchRow = NULL;
            while ((fetchRow = mysql_fetch_row(tables)) != NULL) {
                if (strcmp(fetchRow[fields - 1], "student") == 0)
                    flag = 1;
                printf("table : %-10s\n", fetchRow[fields - 1]);
            }
            if (!flag) {
                FILE *file = NULL;
                file = fopen("student.sql", "r");
                if (file) {
                    while (!feof(file)) {
                        char str[64] = {0};
                        fgets(str, sizeof(str), file);
                        strcat(sql, str);
                    }
                    printf("%s\n", sql);
                } else {
                    fprintf(stderr, "fopen student.sql error\n");
                    exit(EXIT_FAILURE);
                }
                fclose(file);
                int r = mysql_real_query(mysql, sql, (unsigned long) strlen(sql));
                if (r == 0) {
                    printf("table student was created success !! \n");
                } else {
                    printf("table student was created error !! \n");
                    exit(EXIT_FAILURE);
                }
            } else {
                printf("table student already exists !! \n");
            }
        } else {
            printf("something wrong\n");
        }
    }

    /////////////////////////////////////////////////////////////////

    memset(sql, 0, sizeof(sql));
    sprintf(sql, "select count(id) from student");
    int count = 0;
    if (mysql_real_query(mysql, sql, (unsigned int) strlen(sql)) == 0) {
        MYSQL_RES *result = mysql_store_result(mysql);
//        my_ulonglong rows = mysql_num_rows(result);
        unsigned int fields = mysql_num_fields(result);
        MYSQL_ROW fetchRow = mysql_fetch_row(result);
        count = atoi(fetchRow[fields - 1]);
        printf("count %d\n", count);
        mysql_free_result(result);
    }
    if (count 

理论要掌握,实操不能落!以上关于《C 扩展库 - mysql API CRUD》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表