数据类型

  1. 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;
  2. 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 <stdio.h>
#include <mysql.h> int main(int argc, const char *argv[])
{
MYSQL mysql; if (NULL == mysql_init(&mysql)) { //分配和初始化MYSQL对象
printf("mysql_init(): %s\n", 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\n", mysql_error(&mysql));
return -1;
} printf("Connected MySQL successful! \n"); mysql_close(&mysql);
return 0;
}

2 数据查询语句函数mysql_query

函数原型

int STDCALL mysql_query(MYSQL *mysql, const char *q);

查询的例子:

#include <stdio.h>
#include <mysql.h>
#include <string.h>
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\n", mysql_error(&mysql));
return -1;
}
if (NULL == mysql_real_connect(&mysql,
"localhost",
"root",
"shallnet",
"db_users",
0,
NULL,
0)) {
printf("mysql_real_connect(): %s\n", mysql_error(&mysql));
return -1;
}
printf("1. Connected MySQL successful! \n");
query_str = "select * from tb_users";
rc = mysql_real_query(&mysql, query_str, strlen(query_str));
if (0 != rc) {
printf("mysql_real_query(): %s\n", mysql_error(&mysql));
return -1;
}
res = mysql_store_result(&mysql);
if (NULL == res) {
printf("mysql_restore_result(): %s\n", mysql_error(&mysql));
return -1;
}
rows = mysql_num_rows(res);
printf("The total rows is: %d\n", rows);
fields = mysql_num_fields(res);
printf("The total fields is: %d\n", fields);
while ((row = mysql_fetch_row(res))) {
for (i = 0; i < fields; i++) {
printf("%s\t", row[i]);
}
printf("\n");
}
mysql_close(&mysql);
return 0;
}

插入删除的例子:

#include <stdio.h>
#include <mysql.h>
#include <string.h>
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\n", mysql_error(&mysql));
return -1;
}
if (NULL == mysql_real_connect(&mysql,
"localhost",
"root",
"shallnet",
"db_users",
0,
NULL,
0)) {
printf("mysql_real_connect(): %s\n", mysql_error(&mysql));
return -1;
}
printf("1. Connected MySQL successful! \n");
query_str = "select * from tb_users";
rc = mysql_real_query(&mysql, query_str, strlen(query_str));
if (0 != rc) {
printf("mysql_real_query(): %s\n", mysql_error(&mysql));
return -1;
}
res = mysql_store_result(&mysql);
if (NULL == res) {
printf("mysql_restore_result(): %s\n", mysql_error(&mysql));
return -1;
}
rows = mysql_num_rows(res);
printf("The total rows is: %d\n", rows);
fields = mysql_num_fields(res);
printf("The total fields is: %d\n", fields);
while ((row = mysql_fetch_row(res))) {
for (i = 0; i < fields; i++) {
printf("%s\t", row[i]);
}
printf("\n");
}
mysql_close(&mysql);
return 0;
}
gcc具体的编译方法:gcc ***.c -o *** -lmysqlclient

参考

linux下C语言编程操作MySQL数据库

C语言对Mysql函数操作的更多相关文章

  1. mysql函数操作-增加自定义函数

    #首先执行看mysql是否开启函数SHOW VARIABLES LIKE '%func%';------------------------------------------------------ ...

  2. mysql函数操作(6)

    <?php try{ $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); }catc ...

  3. mysql函数操作(5)

    <?php try{ $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); }catc ...

  4. mysql函数操作(3)

    <?php $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); $dbh->s ...

  5. mysql函数操作(2)

    <?php $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); $dbh->s ...

  6. mysql函数操作

    <?php try{ $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); }catc ...

  7. MySQL函数操作数据库

    1.select语句查询信息(实现模糊查询) <form name="form1" method="post" action=""&g ...

  8. mysql函数操作(4)

    <?php ... $query = "INSERT INTO contactInfo (name, address, phone) VALUES (?, ?, ?)"; $ ...

  9. 新手学python(2):C语言调用完成数据库操作

    继续介绍本人的python学习过程.本节介绍如何利用python调用c代码.内容还是基于音乐信息提取的过程,架构如图一.Python调用c实现的功能是利用python访问c语言完成mysql数据库操作 ...

随机推荐

  1. JVM调优——JVM监控工具jvisualvm的使用及GC插件安装

    一.前言 在高并发的场景下,我们网站的的访问性能会降低,我们怎么优化,这是个问题!天天听JVM调优,实际上还是不知道怎么调优,调优也是看着网上说的修改一下JVM的堆的空间等等进行的.实际上我们应该在压 ...

  2. RISC-V 特权指令结构

    机器模式 机器模式(缩写为 M 模式,M-mode)是 RISC-V 中 hart(hardware thread,硬件线 程)可以执行的最高权限模式.在 M 模式下运行的 hart 对内存,I/O ...

  3. suse 12 双网卡bonding模式

    文章目录 工欲善其事,必先配静态ip 网卡bonding eth0 eth1 192.168.70.52 192.168.70.55 工欲善其事,必先配静态ip 重要的事情说三遍 修改配置之前,先备份 ...

  4. [数分笔记]Dedekind切割定理的证明

    1.定理内容 Dedekind切割定理:设是实数集的一个切割,则或者有最大数,或者有最小数. 2.证明过程 设是中所有有理数所构成的集合,是中所有有理数所构成的集合 从而构成一个有理数集的切割 有三种 ...

  5. windows消息机制框架原理【简单版本】

    windows消息机制框架原理 结合两张图理解 窗口和窗口类 Windows UI 应用程序 (e) 具有一个主线程 (g).一个或多个窗口 (a) 和一个或多个子线程 (k) [工作线程或 UI 线 ...

  6. windows server 2012 r2 修改administrator密码

    转至:https://jingyan.baidu.com/article/b907e627b615b646e7891cbf.html 1.进入开始面板,点击"管理工具". 2.双击 ...

  7. 2016EC Final F.Mr. Panda and Fantastic Beasts

    题目大意 \(T(1\leq T\leq42)\)组数据,给定\(n(2\leq n\leq 50000)\)个字符串\(S_{i}(n\leq\sum_{i=1}^{n}S_{i}\leq 2500 ...

  8. Activity的创建及生命周期

  9. Docker-可视化管理工具总结-推荐使用Portainer

    对于初学docker的小白,一款好的可视化工具有助于快速掌握docker基本形态和概念,下面针对docker可视化工具做些总结 ui-for-docker UI For Docker是一个使用Dock ...

  10. HTTP请求过程和状态响应码

    HTTP请求过程 我们在浏览器中输入一个URL,回车之后便可以在浏览器中观察到页面内容.实际上,这个过程是浏览器向网站所在的服务器发送了一个请求,网站服务器接收到这个请求后进行处理和解析,然后返回对应 ...