mysql C语言API接口及实例

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

Mysql数据库动态库: libmysql.dll  libmysql.lib mysql.h  WinSock2.h

Mysql API数据结构   (mysql.h)

       MYSQL:连接数据库前,必须先创建MYSQL变量,此变量在很多Mysql API函数会用到。它包含了一些连接信息等数据。

       MYSQL_RES:MYSQL_RES结构体中包含了查询结果集,也就是从数据库中查询到的数据。可以使用mysql_store_resultmysql_use_result函数获得。

       MYSQL_ROW:MYSQL ROW的定义如下:typedef char **MYSQL_ROW;可见,它实际上是char **类型,指向一个字符串数组。存储一行中各段字符数组,可以通过mysql_fetch_row函数获得。

       MYSQL_FIELD:MYSQL_FIELD中包含了字段名、字段类型和大小等信息。可以重复调用mysql_fetch_field函数获得所有字段的信息。

 

     Mysql C API编程步骤

1、首先我们要包含mysql的头文件,并链接mysql动态库。

#include <WinSock2.h>      // 进行网络编程需要winsock2.h   

#include <mysql.h>   

#pragma comment(lib, "libmysql.lib")  

 2、创建MYSQL变量。如:

       MYSQL mysql;

3、初始化MYSQL变量。

       mysql_init(&mysql);

 4、调用mysql_real_connect函数连接Mysql数据库。

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);    

       参数说明:mysql--前面定义的MYSQL变量;host--MYSQL服务器的地址;user--登录用户名;passwd--登录密码;db--要连接的数据库;port--MYSQL服务器的TCP服务端口;unix_socket--unix连接方式,为NULL时表示不使用socket或管道机制;clientflag--Mysql运行为ODBC数据库的标记,一般取0

       连接失败时该函数返回0

 5、调用mysql_real_query函数进行数据库查询。mysql_real_query函数的原型如下:

       int  STDCALL mysql_real_query(MYSQL *mysql, const char *q, unsigned long length);

       参数说明:mysql--前面定义的MYSQL变量;q--SQL查询语句;length--查询语句的长度。

       查询成功则该函数返回0

       6、通过调用mysql_store_resultmysql_use_result函数返回的MYSQL_RES变量获取查询结果数据。

       两个函数的原型分别为:

       MYSQL_RES *     STDCALL mysql_store_result(MYSQL *mysql);

       MYSQL_RES *     STDCALL mysql_use_result(MYSQL *mysql);

       这两个函数分别代表了获取查询结果的两种方式。第一种,调用mysql_store_result函数将从Mysql服务器查询的所有数据都存储到客户端,然后读取;第二种,调用mysql_use_result初始化检索,以便于后面一行一行的读取结果集,而它本身并没有从服务器读取任何数据,这种方式较之第一种速度更快且所需内存更少,但它会绑定服务器,阻止其他线程更新任何表,而且必须重复执行mysql_fetch_row读取数据,直至返回NULL,否则未读取的行会在下一次查询时作为结果的一部分返回,故经常我们使用mysql_store_result

       7、调用mysql_fetch_row函数读取结果集数据。

       上述两种方式最后都是重复调用mysql_fetch_row函数读取数据。mysql_fetch_row函数的原型如下:

       MYSQL_ROW STDCALL mysql_fetch_row(MYSQL_RES *result);

       参数result就是mysql_store_resultmysql_use_result的返回值。

       该函数返回MYSQL_ROW型的变量,即字符串数组,假设为row,则row[i]为第i个字段的值。当到结果集尾部时,此函数返回NULL

       8、结果集用完后,调用mysql_free_result函数释放结果集,以防内存泄露。mysql_free_result函数的原型如下:

       void  STDCALL mysql_free_result(MYSQL_RES *result);

       9、不再查询Mysql数据库时,调用mysql_close函数关闭数据库连接。mysql_close函数的原型为:

       void STDCALL mysql_close(MYSQL *sock);

 

例子:

1. int main()  

2.     MYSQL mysql;  

3.     MYSQL_RES *res;  

4.     MYSQL_ROW row;  

5.     mysql_init(&mysql);  // 初始化MYSQL变量  

6.     // 连接Mysql服务器,本例使用本机作为服务器。访问的数据库名称为"msyql",参数中的user为你的登录用户名,***为登录密码,需要根据你的实际用户进行设置  

7.     if (!mysql_real_connect(&mysql, "127.0.0.1""user""123""mysql", 3306, 0, 0))  {  

8.         cout << "mysql_real_connect failure!" << endl;  

9.         return 0;  

10.     }  

11.     if (mysql_real_query(&mysql, "select * from user", (unsigned long)strlen("select * from user"))){    // 查询mysql数据库中的user 

12.         cout << "mysql_real_query failure!" << endl;  

13.         return 0;  

14.     }  // 存储结果集  

15.     res = mysql_store_result(&mysql);  

16.     if (NULL == res) {  

17.         cout << "mysql_store_result failure!" << endl;  

18.         return 0;  

19.     }  

20.     // 重复读取行,并输出第一个字段的值,直到rowNULL  

21.     while (row = mysql_fetch_row(res))  {  

22.         cout << row[0] << endl;  

23.     }  

24.     mysql_free_result(res);  // 释放结果集   

25.     mysql_close(&mysql);  // 关闭Mysql连接

26.       return 0;  }  

 

10.Char * mysql_get_client_info() 显示mysql客户端版本

  MySQL client version: 5.0.38

11.int mysql_num_fields(MYSQL_RES *result) 返回结果子表中域(字段)的个数

12. MYSQL_FIELD * mysql_fetch_field(MYSQL_RES *result) 返回结果子表中的域结构体指针

13.Void mysql_real_escape_string(MYSQL* con, char* savedata, char *data, int size) 在将二进制数据(非文本)保存到数据库之前,需要转义,否则数据库不能正常保存,取出数据时,无需解转移。转义时一个字符转义后2个字符,所以savedata内存必须为data2

14. unsigned long * mysql_fetch_lengths(MYSQL_RES *result) 获取结果中各个字符串的长度,返回为1维数组

 

一些有用的例子:

#include 

#include 

 

int main(int argc, char **argv)

{

 

  MYSQL *conn;

  MYSQL_RES *result;

  MYSQL_ROW row;

  int num_fields;

  int i;

 

  conn = mysql_init(NULL);

  mysql_real_connect(conn, "localhost", "zetcode", "passwd", "testdb", 0, NULL, 0);

 

  mysql_query(conn, "SELECT * FROM writers");

  result = mysql_store_result(conn);

 

  num_fields = mysql_num_fields(result);

 

  while ((row = mysql_fetch_row(result)))

  {

      for(i = 0; i < num_fields; i++)

      {

          printf("%s ", row[i] ? row[i] : "NULL");

      }

      printf("\n");

  }

 

  mysql_free_result(result);

  mysql_close(conn);

 

}

The example prints all names from the writers table.

$ ./select

Leo Tolstoy

Jack London

Honore de Balzac

Lion Feuchtwanger

Emile Zola

 mysql_query(conn, "SELECT * FROM writers");

We execute the query, that will retrieve all names from the writers database.

 result = mysql_store_result(conn);

We get the result set.

 num_fields = mysql_num_fields(result);

We get the number of fields in the table.

 while ((row = mysql_fetch_row(result)))

 {

     for(i = 0; i < num_fields; i++)

     {

         printf("%s ", row[i] ? row[i] : "NULL");

     }

     printf("\n");

 }

We fetch the rows and print them to the screen.

 mysql_free_result(result);

We free the resources.

Column headers

In the next example, we will retrieve data and show the their column names from the table.

For this, we will create a new table friends.

 mysql> create table friends (id int not null primary key auto_increment,

                               name varchar(20), age int);

 mysql> insert into friends(name, age) values('Tom', 25);

 mysql> insert into friends(name, age) values('Elisabeth', 32);

 mysql> insert into friends(name, age) values('Jane', 22);

 mysql> insert into friends(name, age) values('Luke', 28);

We insert some data into the table.

#include 

#include 

 

int main(int argc, char **argv)

{

 

  MYSQL *conn;

  MYSQL_RES *result;

  MYSQL_ROW row;

  MYSQL_FIELD *field;

 

  int num_fields;

  int i;

 

  conn = mysql_init(NULL);

  mysql_real_connect(conn, "localhost", "zetcode", "passwd", "testdb", 0, NULL, 0);

 

  mysql_query(conn, "SELECT * FROM friends");

  result = mysql_store_result(conn);

 

  num_fields = mysql_num_fields(result);

 

  while ((row = mysql_fetch_row(result)))

  {

      for(i = 0; i < num_fields; i++)

      {

          if (i == 0) {

             while(field = mysql_fetch_field(result)) {

                printf("%s ", field->name);

             }

          printf("\n");

          }

          printf("%s  ", row[i] ? row[i] : "NULL");

      }

  }

  printf("\n");

 

  mysql_free_result(result);

  mysql_close(conn);

}

The example is similar to the previous one. It just adds column header names to it.

 while(field = mysql_fetch_field(result)) {

     printf("%s ", field->name);

 }

The mysql_fetch_field() call returns a MYSQL_FIELD structure. We get the column header names from this structure.

$ ./headers

id name age

1  Tom  25

2  Elisabeth  32

3  Jane  22

4  Luke  28

And this is the output of our program.

Inserting images into MySQL database

Some people prefer to put their images into the database, some prefer to keep them on the file system for their applications. Technical difficulties arise when we work with millions of images. Images are binary data. MySQL database has a special data type to store binary data called BLOB (Binary Large Object).

mysql> describe images;

+-------+------------+------+-----+---------+-------+

| Field | Type       | Null | Key | Default | Extra |

+-------+------------+------+-----+---------+-------+

| id    | int(11)    | NO   | PRI |         |       |

| data  | mediumblob | YES  |     | NULL    |       |

+-------+------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

This is the table, that we will use in our example. It can be created by the following SQL statement.

create table images(id int not null primary key, data mediumblob);

#include 

#include 

 

int main(int argc, char **argv)

{

  MYSQL *conn;

 

  int len, size;

  char data[1000*1024];

  char chunk[2*1000*1024+1];

  char query[1024*5000];

 

  FILE *fp;

 

  conn = mysql_init(NULL);

  mysql_real_connect(conn, "localhost", "zetcode", "passwd", "testdb", 0, NULL, 0);

 

  fp = fopen("image.png", "rb");

  size = fread(data, 1, 1024*1000, fp);

 

  mysql_real_escape_string(conn, chunk, data, size);

 

  char *stat = "INSERT INTO images(id, data) VALUES('1', '%s')";

  len = snprintf(query, sizeof(stat)+sizeof(chunk) , stat, chunk);

 

  mysql_real_query(conn, query, len);

 

  fclose(fp);

  mysql_close(conn);

}

In this example, we will insert one image into the images table. The image can be max 1 MB.

 fp = fopen("image.png", "rb");

 size = fread(data, 1, 1024*1000, fp);

Here we open the image and read it into the data array.

 mysql_real_escape_string(conn, chunk, data, size);

Binary data can obtain special characters, that might cause troubles in the statements. We must escape them. The mysql_real_escape_string() puts the encoded data into the chunk array. In theory, every character might be a special character. That's why the chunk array two times as big as the data array. The function also adds a terminating null character.

 char *stat = "INSERT INTO images(id, data) VALUES('1', '%s')";

 len = snprintf(query, sizeof(stat)+sizeof(chunk) , stat, chunk);

These two code lines prepare the MySQL query.

 mysql_real_query(conn, query, len);

Finally, we execute the query.

Selecting images from MySQL database

In the previous example, we have inserted an image into the database. In the following example, we will select the inserted image back from the database.

#include 

#include 

 

int main(int argc, char **argv)

{

  MYSQL *conn;

  MYSQL_RES *result;

  MYSQL_ROW row;

 

  unsigned long *lengths;

  FILE *fp;

 

  conn = mysql_init(NULL);

  mysql_real_connect(conn, "localhost", "zetcode", "passwd", "testdb", 0, NULL, 0);

 

  fp = fopen("image.png", "wb");

 

  mysql_query(conn, "SELECT data FROM images WHERE id=1");

  result = mysql_store_result(conn);

 

  row = mysql_fetch_row(result);

  lengths = mysql_fetch_lengths(result);

 

  fwrite(row[0], lengths[0], 1, fp);

  mysql_free_result(result);

 

  fclose(fp);

  mysql_close(conn);

}

In this example, we will create an image file from the database.

 fp = fopen("image.png", "wb");

We open a file for writing.

 mysql_query(conn, "SELECT data FROM images WHERE id=1");

We select an image with id 1.

 row = mysql_fetch_row(result);

The row contains raw data.

 lengths = mysql_fetch_lengths(result);

We get the length of the image.

 fwrite(row[0], lengths[0], 1, fp);

We create the image file using the fwrite() standard function call. 

 



本文转自 a_liujin 51CTO博客,原文链接:http://blog.51cto.com/a1liujin/1686202,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
17天前
|
缓存 前端开发 API
API接口封装系列
API(Application Programming Interface)接口封装是将系统内部的功能封装成可复用的程序接口并向外部提供,以便其他系统调用和使用这些功能,通过这种方式实现系统之间的通信和协作。下面将介绍API接口封装的一些关键步骤和注意事项。
|
24天前
|
监控 前端开发 JavaScript
实战篇:商品API接口在跨平台销售中的有效运用与案例解析
随着电子商务的蓬勃发展,企业为了扩大市场覆盖面,经常需要在多个在线平台上展示和销售产品。然而,手工管理多个平台的库存、价格、商品描述等信息既耗时又容易出错。商品API接口在这一背景下显得尤为重要,它能够帮助企业在不同的销售平台之间实现商品信息的高效同步和管理。本文将通过具体的淘宝API接口使用案例,展示如何在跨平台销售中有效利用商品API接口,以及如何通过代码实现数据的统一管理。
|
1月前
|
API 数据库 C语言
【C/C++ 数据库 sqlite3】SQLite C语言API返回值深入解析
【C/C++ 数据库 sqlite3】SQLite C语言API返回值深入解析
170 0
|
1月前
|
安全 算法 API
产品经理必备知识——API接口
前言 在古代,我们的传输信息的方式有很多,比如写信、飞鸽传书,以及在战争中使用的烽烟,才有了著名的烽火戏诸侯,但这些方式传输信息的效率终究还是无法满足高速发展的社会需要。如今万物互联的时代,我通过一部手机就可以实现衣食住行的方方面面,比如:在家购物、远程控制家电、自动驾驶等等,背后都离不开我们今天要聊的API接口。
|
1月前
|
数据采集 JSON API
如何实现高效率超简洁的实时数据采集?——Python实战电商数据采集API接口
你是否曾为获取重要数据而感到困扰?是否因为数据封锁而无法获取所需信息?是否因为数据格式混乱而头疼?现在,所有这些问题都可以迎刃而解。让我为大家介绍一款强大的数据采集API接口。
|
1月前
|
安全 API 数据安全/隐私保护
API接口知识小结
应用程序接口API(Application Programming Interface),是提供特定业务输出能力、连接不同系统的一种约定。这里包括外部系统与提供服务的系统(中后台系统)或后台不同系统之间的交互点。包括外部接口、内部接口,内部接口又包括:上层服务与下层服务接口、同级接口。
|
1天前
|
API 开发者
邮件API接口使用的方法和步骤
AOKSEND指南:了解和使用邮件API接口,包括选择适合的接口(如AOKSEND、Mailgun、SMTP),获取访问权限,配置发件人、收件人及邮件内容,调用接口发送邮件,并处理返回结果,以高效集成邮件功能。
|
4天前
|
Java API Android开发
[NDK/JNI系列04] JNI接口方法表、基础API与异常API
[NDK/JNI系列04] JNI接口方法表、基础API与异常API
11 0
|
7天前
|
XML JSON API
快速淘宝商品详情页面API接口传输 php
PI(Application Programming Interface,应用程序接口)是一组预定义的函数、协议和工具,用于构建软件应用程序之间的交互。它允许不同的软件系统和应用通过统一的接口进行数据交换和通信
|
11天前
|
人工智能 API 开发者
免费使用Kimi的API接口,kimi-free-api真香
今年AI应用兴起,各类智能体涌现,但API免费额度有限。为解决这一问题,GitHub上的[kimi-free-api](https://github.com/LLM-Red-Team/kimi-free-api)项目提供了方便,支持高速流式输出、多轮对话等,与ChatGPT接口兼容。此外,还有其他大模型的免费API转换项目,如跃问StepChat、阿里通义Qwen等。该项目可帮助用户免费体验,通过Docker-compose轻松部署。只需获取refresh_token,即可开始使用。这个开源项目促进了AI学习和开发,为探索AI潜力提供了新途径。
232 2