【MySQL】使用C/C++连接MySQL数据库
- 验证
- 使用
- select特殊点
本文目的:使用MySQL提供的CAPI完成对数据库的操作
验证
#include <iostream>
#include <mysql/mysql.h>
int main()
{
std::cout<<"mysql cilent version: "<<mysql_get_client_info()<<std::endl;
return 0;
}
注意:使用mysqlclient库需要指明库的位置-L和库的名称-l
[wmh@pDaD Test]$ g++ test.cc -o test -L/lib64/mysql -lmysqlclient
[wmh@pDaD Test]$ ./test
mysql cilent version: 5.7.42
使用
MySQL-5.7API接口介绍
#include <iostream>
#include <mysql/mysql.h>
#include <string>
#include <unistd.h>
const std::string host="127.0.0.1";
const std::string user = "connection";
const std::string passwd="123456";
const std::string db="scott";
const unsigned int port = 3306;
int main()
{
MYSQL* mysql = mysql_init(nullptr);//初始化MySQL句柄
if(nullptr == mysql)
{
std::cerr<<"init MySQL error"<<std::endl;
return 1;
}
if(mysql_real_connect(mysql,host.c_str(),user.c_str(),passwd.c_str(),db.c_str(),port,nullptr,0) == nullptr)//连接MySQL数据库
{
std::cerr<<"connect MySQL error"<<std::endl;
return 2;
}
mysql_set_character_set(mysql,"utf8");//设置字符集编码
std::string sql="update user set age='60' where id=3";
int n = mysql_query(mysql,sql.c_str());//下发命令
if(n==0) std::cout<<sql<<"success"<<std::endl;
else std::cout<<sql<<"failed"<<std::endl;
mysql_close(mysql);
return 0;
}
mysql> select* from user;
+----+-----+-----------+
| id | age | name |
+----+-----+-----------+
| 1 | 28 | 赵志敬 |
| 3 | 18 | 杨过 |
| 4 | 16 | 小龙女 |
+----+-----+-----------+
mysql> select* from user;
+----+-----+-----------+
| id | age | name |
+----+-----+-----------+
| 1 | 28 | 赵志敬 |
| 3 | 60 | 杨过 |
| 4 | 16 | 小龙女 |
+----+-----+-----------+
select特殊点
sql执行完以后,如果是查询语句,我们要能看到数据显示出来,那么就需要mysql_store_result这个函数来保存结果到本地
MYSQL_RES *mysql_store_result(MYSQL *mysql);
获取结果行数mysql_num_rows
my_ulonglong mysql_num_rows(MYSQL_RES *res);
获取结果列数mysql_num_fields
unsigned int mysql_num_fields(MYSQL_RES *res);
以列为单位获取列属性mysql_fetch_fields
MYSQL_FIELD *mysql_fetch_fields(MYSQL_RES *res);
typedef struct st_mysql_field {
char *name; /* Name of column */
char *org_name; /* Original column name, if an alias */
char *table; /* Table of column if column was a field */
char *org_table; /* Org table name, if table was an alias */
char *db; /* Database for table */
char *catalog; /* Catalog for table */
char *def; /* Default value (set by mysql_list_fields) */
unsigned long length; /* Width of column (create length) */
unsigned long max_length; /* Max width for selected set */
unsigned int name_length;
unsigned int org_name_length;
unsigned int table_length;
unsigned int org_table_length;
unsigned int db_length;
unsigned int catalog_length;
unsigned int def_length;
unsigned int flags; /* Div flags */
unsigned int decimals; /* Number of decimals in field */
unsigned int charsetnr; /* Character set */
enum enum_field_types type; /* Type of field. See mysql_com.h for types */
void *extension;
} MYSQL_FIELD;
以行为单位获取结果内容mysql_fetch_row
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
MYSQL_ROW其实就是char **
.当成一个二维数组
释放结果集内存
void mysql_free_result(MYSQL_RES *result)
调用mysql_store_result返回结构体,这个函数malloc了一片内存空间来存储查询过来的数据,需要free掉
整体示例
#include <iostream>
#include <mysql/mysql.h>
#include <string>
#include <unistd.h>
const std::string host = "127.0.0.1";
const std::string user = "connection";
const std::string passwd = "123456";
const std::string db = "scott";
const unsigned int port = 3306;
int main()
{
MYSQL *mysql = mysql_init(nullptr); // 初始化MySQL句柄
if (nullptr == mysql)
{
std::cerr << "init MySQL error" << std::endl;
return 1;
}
if (mysql_real_connect(mysql, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr) // 连接MySQL数据库
{
std::cerr << "connect MySQL error" << std::endl;
return 2;
}
mysql_set_character_set(mysql, "utf8"); // 设置字符集编码
// std::string sql="update user set age='60' where id=3";
std::string sql = "select * from user";
int n = mysql_query(mysql, sql.c_str()); // 下发命令
if (n == 0)
std::cout << sql << "success" << std::endl;
else
std::cout << sql << "failed" << std::endl;
// 保存结果集转储到res中
MYSQL_RES *res = mysql_store_result(mysql);
if (res == nullptr)
{
std::cerr << "mysql_store_result_error" << std::endl;
}
int rows = mysql_num_rows(res); // 获取行数
int fields = mysql_num_fields(res); // 获取列数
std::cout << "行: " << rows << std::endl;
std::cout << "列:" << fields << std::endl;
// 获取列属性
MYSQL_FIELD *fields_array = mysql_fetch_fields(res);
for (int i = 0; i < fields; ++i)
{
std::cout << fields_array[i].name << "\t"; // 打印列名
}
std::cout << std::endl;
for (int i = 0; i < rows; i++)
{
//row可以认为是一个char** 指针
MYSQL_ROW row = mysql_fetch_row(res);//以行为单位获取记录
for (int j = 0; j < fields; ++j)
{
std::cout << row[j] << "\t";
}
std::cout << "\n";
}
std::cout << fields_array[0].db << " " << fields_array[0].table << std::endl;
mysql_free_result(res);//释放结果集
mysql_close(mysql);//关闭MYSQL句柄
return 0;
}