C 扩展库

Wesley13
• 阅读 773

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 <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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 < 50) {
        memset(sql, 0, sizeof(sql));
        for (i = 0; i < 10; ++i) {
            char name[16] = {0};
            char address[64] = {0};
            sprintf(name, "zing-%d", i);
            sprintf(address, "jiangsu-wuxi-%d", i);
            sprintf(sql, "INSERT INTO `student` (`name`,`age`,`address`) VALUES('%s','%d','%s')", name, 10 + i,
                    address);
            if (mysql_real_query(mysql, sql, (unsigned int) strlen(sql)) == 0) {
                my_ulonglong id = mysql_insert_id(mysql);
                printf("insert num %ld success\n", (long int) id);
            }
        }
    }
    memset(sql, 0, sizeof(sql));
    sprintf(sql, "select * from student order by id asc limit 0,20");
    if (mysql_real_query(mysql, sql, (unsigned int) strlen(sql)) == 0) {
        MYSQL_RES *result = mysql_store_result(mysql);
        unsigned int fields = mysql_num_fields(result);
        printf("fields %d\n", fields);
        MYSQL_ROW fetchRow;
        while ((fetchRow = mysql_fetch_row(result)) != NULL) {
            printf("id:%s name:%s age:%s address:%s \n", fetchRow[0], fetchRow[1], fetchRow[2], fetchRow[3]);
        }
        mysql_free_result(result);
    }
    ///////////////////////////////////////////////////////////////////////////////////////
    memset(sql, 0, sizeof(sql));
    sprintf(sql, "update student set name='%s',age=%d,address='%s' where id between %d and %d;", "zhangrongxiang", 24,
            "jiangsu-xuzhou-1", 1, 10);
    if (mysql_real_query(mysql, sql, (int) strlen(sql)) == 0) {
        printf("mysql_affected_rows : %d\n", (int) mysql_affected_rows(mysql));
    }

    mysql_close(mysql);
    mysql_library_end();
    return EXIT_SUCCESS;
}

/*
 *
 * Initialize the MySQL client library by calling mysql_library_init().
 * This function exists in both the libmysqlclient C client library and the libmysqld embedded server library,
 * so it is used whether you build a regular client program by linking with the -libmysqlclient flag, or an embedded server application by linking with the -libmysqld flag.
 *
 * Initialize a connection handler by calling mysql_init() and connect to the server by calling mysql_real_connect().
 *
 * Issue SQL statements and process their results. (The following discussion provides more information about how to do this.)
 *
 * Close the connection to the MySQL server by calling mysql_close().
 *
 * End use of the MySQL client library by calling mysql_library_end().
 */
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Easter79 Easter79
3年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这