数据类型

  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. suse 12安装 python-pip

    文章目录 方法一 安装setup-tools 安装pip 方法二 配置阿里云pip源 pip安装pyotp 方法一 安装setup-tools linux-oz6w:~ # wget https:// ...

  2. Python语法进阶(2)- 正则表达式

    1.初识正则表达式 1.1.什么是正则表达式 正则表达式是一个特殊的字符序列,便于检查一个字符串是否与某种模式匹配:应用于字符串,在字符串中通过复杂的过滤筛选等操作得到我们想要的数据: 正则表达式的特 ...

  3. Nginx-反向代理服务器

    概述 Nginx是一款轻量级的Web服务器.反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用. 优点 nginx是多进程的,不会出现并发问题,不用加锁.一个进程出问题 ...

  4. 乘风破浪,遇见未来元宇宙(Metaverse)之进入元宇宙世界,虚拟数字人行业洞察报告

    正值元宇宙热潮,虚拟数字人兴起 作为⼀个新兴领域,虚拟数字⼈已经引起市场和资本的⾼度关注,截⾄目前据不完全统计,全球范围已有500+虚拟数字人相关项目获得融资,融资总额超10亿美元,并且融资项目和总额 ...

  5. 不用rustup,Windows下gnu版Rust安装与开发环境配置

    写在前面 本文介绍了在不使用rustup的情况下,在Windows上安装gnu版的Rust,并配置开发环境(VSCode + rust-analyzer,CLion + IntelliJ Rust)的 ...

  6. 【外企测试面试、笔试】分享下历时8轮、30k+的外企面试全过程

    外企福利 薪酬体系完善(期权.股票等),定期薪酬市场调研,紧跟一线互联网大厂 加班很少很少 年假多,15-20天 国外免费旅游.旅游金 免费培训英语(还可能出国培训) 定期技术交流 免费零食 定期团建 ...

  7. windows痕迹清理的基本思路和思考逻辑

    1.痕迹清理的基本概念 在渗透测试的过程结束后清理自己在从开始接触到目标计算机是开始所有操作的痕迹 2.痕迹清理的目的 为下一步的渗透测试拖延时间 提高隐蔽性 所有的痕迹清理都不是绝对的,只要和计算机 ...

  8. 教你快速区分传统报表和商业智能BI

    很多人分不清楚,传统报表和商业智能BI之间的区别?有些人认为,BI就是做报表的,其实不然,报表只是BI的一部分,报表是关于过去和现状的展示,而BI是关于如何通过分析数据,帮助决策者找到改变和提高的方案 ...

  9. 使用教程:宝塔服务器管理助手Linux面版

    网页提示:宝塔Linux面板初始化成功,点击登陆页面:直接使用初始化配置时填写的帐号及密码登陆面板功能:网站管理.FTP管理.数据库管理.系统安全.文件管理.计划任务.环境设置. 方法/步骤1: 使用 ...

  10. 【C# IO 操作 】IFormatProvider接口|IFormattable 接口 格式化接口

    IFormatProvider接口获取一个满足要求的个格式化器. 方法 object? GetFormat(Type? formatType);GetFormat方法主要提供一个满足指定要求的对象,该 ...