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().
*/

C 扩展库 - mysql API CRUD的更多相关文章

  1. C 扩展库 - mysql API

    MySQL API C API Data Structures MYSQL This structure represents handler for one database connection. ...

  2. C 扩展库 - mysql API general outline

    Application programs should use this general outline for interacting with MySQL Initialize the MySQL ...

  3. C 扩展库 - sqlite3 API CRUD

    CRUD struct student typedef struct STUDENT { unsigned int id; unsigned char name[16]; unsigned int a ...

  4. C 扩展库 - sqlite3 API

    sqlite3 API Summary sqlite3 The database connection object. Created by sqlite3_open() and destroyed ...

  5. 【小结】有关mysql扩展库和mysqli扩展库的crud操作封装

    现阶段php如果要操作mysql数据库 php给我们提供了3套库 1.mysql扩展库   面向过程操作 2.mysqli扩展库  面向对象操作和面向过程操作并存  安全性和效率高于mysql扩展库 ...

  6. php数据库编程---mysql扩展库

    1, Java有一种方式操作数据库, PHP有三种方式来操作mysql数据库.(1)mysql扩展库:(2)mysqli扩展库:(3)pdo: 2, mysql扩展库和mysql数据库区别 3, my ...

  7. PHP基础Mysql扩展库

    mysql扩展库操作步骤如下: 1.连接数据库 2.选择数据库 3.设置操作编码 4.发送指令sql,并返回结果集     ddl:数据定义语句     dml:数据操作语句     dql:数据查询 ...

  8. mysql扩展库-1

    启用mysql扩展库 在php.ini文件中去配置mysql扩展库 extension=php_mysql.dll 可以通过 phpinfo() 查看当前php支持什么扩展库. 在sql扩展库中创建一 ...

  9. php笔记08:数据库编程---使用php的MySQL扩展库操作MySQL数据库

    1.使用php的MySQL扩展库操作MySQL数据库: php有3种方式操作MySQL数据库 (1)mysql扩展库 (2)mysqli扩展库 (3)pdo     mysql扩展库与mysql数据库 ...

随机推荐

  1. [Erlang27]如何监控指定目录下的*.beam文件,如果有改动就更新到指定的节点?

    在Erlang In Anger第二章中讲到使用rebar来创建一个Erlang项目(Application或Project) 但美中不足的只是给出了指引,但没有给出详细的步骤. 下面我们就使用reb ...

  2. ssl协议,openssl,创建私有CA

    SSL是Security Socket Layer:安全的套接字层 他介于HTTP和TCP协议层之间 SSL是Netscape公司开发的,属于个人 TLS是标准委员会制定的 OpenSSL是SSL的开 ...

  3. C#——Socket

    最近公司让我搞个socket小程序(服务端). 主要的功能:客户端发字符串到服务端,服务端接收后在界面上显示. 参照了网上许多代码,自己从中修改整理代码. public Form4() { Initi ...

  4. PostgreSQL递归查询

    原料 --创建组织架构表 create table "Org"( "OrgId" ) primary key, "ParentId" ), ...

  5. 在相应目录下新建或读取xml文件

    string path = AppDomain.CurrentDomain.BaseDirectory+"UserContent1.xml"; //判断相应路径下文件是否存在 不存 ...

  6. 重新打造的我的Pugo

    Pugo博客已经搭建了好几个月了吧,给我感受到非常方便的就是,我换了好多VPS,迁移显得非常的方便,但是也有不足的地方,比如发布一篇新的博客,我每次都需要重新进入后台进行新的Post,还需要重新bui ...

  7. python--深浅拷贝 join() 列表和字典的删除 fromkeys建立字典

    北京的冬天雾霾依旧很重,依稀记得人生初见雾霾时的样子,那时的回忆也是有些尴尬,不过雾霾也伴随了我的成长,成为了我肺泡中不可分割的一部分. 今天我想写的是拷贝的问题,不过在这之前我想先补充一点关于字符串 ...

  8. Zipper(动态规划)

    点击打开链接 描述 Given three strings, you are to determine whether the third string can be formed by combin ...

  9. 【Oracle 12c】最新CUUG OCP-071考试题库(59题)

    59.(16-8)choose two: Which two statements are true regarding the USING and ON clauses in table joins ...

  10. 洛谷P1393 动态逆序对(CDQ分治)

    传送门 题解 听别人说这是洛谷用户的双倍经验啊……然而根本没有感觉到……因为另外的那题我是用树状数组套主席树做的……而且莫名其妙感觉那种方法思路更清晰(虽然码量稍稍大了那么一点点)……感谢Candy大 ...