赞
踩
前提:
如果你的mysql是通过yum安装的,那么那些库文件依赖,都是有的,不用你安装了。
但是如果是用 rpm包安装的,就需要去官网下载对应的包。
这些是连接使用的动静态库:
这些是连接使用的头文件:
### makefile test_mysql:mysql_test.cc g++ -o $@ $^ -std=c++11 .PHONY:clean rm -rf test_mysql ### mysql_test.cc #include <iostream> #include <cstdio> #include <mysql/mysql.h> // using namespace std; int main() { std::cout << "client version" << mysql_get_client_info() << std::endl; return 0; }
编译的时候会有报错信息,原因:我们用的函数mysql_get_client_info()
它是在mysqlclient.so
库中,我们使用g++编译器的时候,需要指定链接第三方库的库名字
修改Makefile之后,还是找不到???
排查错误:
发现并没有这个库,暂时不知道是什么原因,我们改用原生的方法!
官网网址:[https://downloads.mysql.com/archives/c-c/]
到官网去下载库文件,原生拷贝
然后上传到Linux中
使用:
tar -小镇微风(xzwf) 名字
mv 将解压后的目录改短一点
再次回到项目即可引用:
建立两个软连接
更新Makefile:
test_mysql:mysql_test.cc
g++ -o $@ $^ -std=c++11 -lmysqlclient -I./include -L./lib
.PHONY:clean
rm -rf test_mysql
终于引入成功了!!!
-I
:用于指明头文件的搜索路径。-L
:用于指明库文件的搜索路径。-l
:用于指明需要连接库文件路径下的哪一个库。
有点激动!花了一个小时!
步骤:
1、连接数据库
2、访问数据库
3、关闭数据库
三步走战略
但是连接之前呢,我们需要有一个对象
0️⃣首先创建一个对象(MYSQL句柄)
MYSQL* mysql_init(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]; my_bool unused1; void *unused2, *unused3, *unused4, *unused5; 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;
1️⃣连接数据库
MYSQL* 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);
注意:
2️⃣CURD
3️⃣关闭数据库
void mysql_close(MYSQL *sock);
#include <iostream> #include <cstdio> #include <string> #include <mysql/mysql.h> const std::string host = "127.0.0.1"; const std::string user = "sjj"; const std::string password = "20020606sjj"; const std::string db = "test_db"; const unsigned int port = 3306; // using namespace std; int main() { // std::cout << "client version " << mysql_get_client_info() << std::endl; // 0.首先要创建mysql句柄 MYSQL *my = mysql_init(nullptr); // 1.连接数据库 if (mysql_real_connect(my, host.c_str(), user.c_str(), password.c_str(), db.c_str(), port, nullptr, 0) == nullptr) { std::cout << "connect faild!" << std::endl; return 1; } std::cout << "connect success!" << std::endl; // 2.中间操作 CURD // 3、 关闭数据库 mysql_close(my); return 0; }
CURD操作:
我们先预设一点数据在student表中
再次插入:
#include <iostream> #include <cstdio> #include <string> #include <mysql/mysql.h> const std::string host = "127.0.0.1"; const std::string user = "sjj"; const std::string password = "20020606sjj"; const std::string db = "test_db"; const unsigned int port = 3306; // using namespace std; int main() { // std::cout << "client version " << mysql_get_client_info() << std::endl; // 0.首先要创建mysql句柄 MYSQL *my = mysql_init(nullptr); // 1.连接数据库 if (mysql_real_connect(my, host.c_str(), user.c_str(), password.c_str(), db.c_str(), port, nullptr, 0) == nullptr) { std::cout << "connect faild!" << std::endl; return 1; } mysql_set_character_set(my, "utf8"); std::cout << "connect success!" << std::endl; // 2.中间操作 CURD std::string sql = "insert into student values (9 ,99,\'王五\')"; int res = mysql_query(my, sql.c_str()); // std::cout << "res = " << res << std::endl; if (res != 0) { std::cout << "execute: " << sql << " faild" << std::endl; return 2; } // 3、 关闭数据库 mysql_close(my); return 0; }
#include <iostream> #include <cstdio> #include <string> #include <mysql/mysql.h> const std::string host = "127.0.0.1"; const std::string user = "sjj"; const std::string password = "20020606sjj"; const std::string db = "test_db"; const unsigned int port = 3306; // using namespace std; int main() { // std::cout << "client version " << mysql_get_client_info() << std::endl; // 0.首先要创建mysql句柄 MYSQL *my = mysql_init(nullptr); // 1.连接数据库 if (mysql_real_connect(my, host.c_str(), user.c_str(), password.c_str(), db.c_str(), port, nullptr, 0) == nullptr) { std::cout << "connect faild!" << std::endl; return 1; } mysql_set_character_set(my, "utf8"); std::cout << "connect success!" << std::endl; // 2.中间操作 CURD //std::string sql = "insert into student values (9 ,99,\'王五\')"; std::string sql = "delete from student where id=9"; int res = mysql_query(my, sql.c_str()); //std::cout << "res = " << res << std::endl; if (res != 0) { std::cout << "execute: " << sql << " faild!" << std::endl; return 2; } std::cout << "execute: " << sql << " success!" << std::endl; // 3、 关闭数据库 mysql_close(my); return 0; }
#include <iostream> #include <cstdio> #include <string> #include <mysql/mysql.h> const std::string host = "127.0.0.1"; const std::string user = "sjj"; const std::string password = "20020606sjj"; const std::string db = "test_db"; const unsigned int port = 3306; // using namespace std; int main() { // std::cout << "client version " << mysql_get_client_info() << std::endl; // 0.首先要创建mysql句柄 MYSQL *my = mysql_init(nullptr); // 1.连接数据库 if (mysql_real_connect(my, host.c_str(), user.c_str(), password.c_str(), db.c_str(), port, nullptr, 0) == nullptr) { std::cout << "connect faild!" << std::endl; return 1; } mysql_set_character_set(my, "utf8"); std::cout << "connect success!" << std::endl; // 2.中间操作 CURD // std::string sql = "insert into student values (9 ,99,\'王五\')"; // std::string sql = "delete from student where id=9"; std::string sql = "update student set name=\'张益达\' where name=\'张三\' "; int res = mysql_query(my, sql.c_str()); // std::cout << "res = " << res << std::endl; if (res != 0) { std::cout << "execute: " << sql << " faild!" << std::endl; return 2; } std::cout << "execute: " << sql << " success!" << std::endl; // 3、 关闭数据库 mysql_close(my); return 0; }
以上三个其实是最基础的操作
select其实是不好处理的,select sql执行完毕,只是第一步,还需要对于数据进行进一步处理解析!!
int mysql_query(MYSQL *mysql,const char* q)
第二个参数是要执行的sql语句,如“ select * from student”
MYSQL_RES *mysql_store_result(MYSQL *mysql)
如果是update、insert语句,那么我们只需要知道操作成功了吗就可以了,但是如果是select 语句,我们还需要读取数据,因为要将数据打印到屏幕上去。如何读取查询数据呢,如果mysql_query语句执行成功,那么我们就通过mysql_store_result来读取结果。
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;
通过下面的函数来读取MYSQL_RES中的数据:
1️⃣获取行数
my_ulonglong mysql_num_rows(MYSQL_RES *res)
2️⃣获取列数
unsigned int mysql_num_fields(MYSQL_RES *res)
int main() { // std::cout << "client version " << mysql_get_client_info() << std::endl; // 0.首先要创建mysql句柄 MYSQL *my = mysql_init(nullptr); // 1.连接数据库 if (mysql_real_connect(my, host.c_str(), user.c_str(), password.c_str(), db.c_str(), port, nullptr, 0) == nullptr) { std::cout << "connect faild!" << std::endl; return 1; } mysql_set_character_set(my, "utf8"); std::cout << "connect success!" << std::endl; // 2.中间操作 CURD std::string sql = "select * from student"; int code = mysql_query(my, sql.c_str()); if (code != 0) { std::cout << "execute: " << sql << " faild!" << std::endl; return 2; } std::cout << "execute: " << sql << " success!" << std::endl; // 这里需要对于数据处理,因为要打印在屏幕上面方便人观看 MYSQL_RES *result= mysql_store_result(my); int rows =mysql_num_rows(result); int cols =mysql_num_fields(result); std::cout<<"行数:"<< rows <<", 列数:"<<cols<<std::endl; free(result); // 3、 关闭数据库 mysql_close(my); return 0; }
3️⃣获取列名
MYSQL_FIELD *mysql_fetch_fields(MYSQL_RES *res)
4️⃣获取结果内容
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result)
它会返回一个MYSQL_ROW变量,MYSQL_ROW其实就是char **
就当成一个二维数组来用吧。
完整代码:
int main() { // std::cout << "client version " << mysql_get_client_info() << std::endl; // 0.首先要创建mysql句柄 MYSQL *my = mysql_init(nullptr); // 1.连接数据库 if (mysql_real_connect(my, host.c_str(), user.c_str(), password.c_str(), db.c_str(), port, nullptr, 0) == nullptr) { std::cout << "connect faild!" << std::endl; return 1; } mysql_set_character_set(my, "utf8"); std::cout << "connect success!" << std::endl; // 2.中间操作 CURD std::string sql = "select * from student"; int code = mysql_query(my, sql.c_str()); if (code != 0) { std::cout << "execute: " << sql << " faild!" << std::endl; return 2; } std::cout << "execute: " << sql << " success!" << std::endl; // 这里需要对于数据处理,因为要打印在屏幕上面方便人观看 MYSQL_RES *result = mysql_store_result(my); // 1--获取行数、列数 int rows = mysql_num_rows(result); int cols = mysql_num_fields(result); std::cout << "行数:" << rows << ", 列数:" << cols << std::endl; // 2--获取表中列名 MYSQL_FIELD *fields = mysql_fetch_fields(result); for (int i = 0; i < cols; i++) { std::cout << fields[i].name << "\t"; } std::cout << std::endl; // 3--获取表中数据 for (int i = 0; i < rows; i++) { MYSQL_ROW line = mysql_fetch_row(result); for (int j = 0; j < cols; j++) { std::cout << line[j] << "\t"; } std::cout << std::endl; } free(result); // 3、 关闭数据库 mysql_close(my); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。