事例机型:ubuntu16.04
1.准备C/C++相关的库和文件
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install mysql-client mysql-server libmysqlclient-dev #libmysqlclient-dev是安装mysql的C/C++库
2.相关知识
#include <mysql/mysql.h>
MYSQL *p; // mysql的连接句柄
mysql_init() // 初始化mysql句柄
mysql_real_connect(MYSQL*mysql, const char* host, const char* user, const char* passwd, const char *dbname, unsigned int port, const char* unix_socket, unsigned long client_flag)
参数:
MYSQL * mysql : 数据库连接句柄 用mysql_init()返回的指针
host : mysql服务器的地址
user: mysql用户名
passwd:mysql密码
dbname:连接的数据库名
port: mysql数据库的连接端口
unix_socket:默认为0
client_flag:一般设置为0
返回:
成功返回MYSQL*连接句柄,失败返回NULL
mysql_query(MYSQL *mysql, const char * sql);
参数:
MYSQL* mysql: mysql连接句柄
const char *sql :sql语句
返回值:失败返回NULL
mysql_store_result(MYSQL *mysql) // 获取查询数据
mysql_free_result(MYSQL_RES *result);// 释放查询事例
mysql_close(MYSQL * mysql); //释放连接句柄...
3.编译命令
g++ *.cpp *.c -lmysqlclient
4.事例
mmysql.h
/*************************************************************************
> File Name: mysql.h
> Author: mouse
> Mail: 2395360675@qq.com
> Created Time: Tue 07 Nov 2017 04:19:27 PM CST
************************************************************************/
#ifndef __MMYSQL_H_
#define __MMYSQL_H_
#include <string>
#include <mysql/mysql.h>
using namespace std;
struct mysqlLogin{
const char* user;
const char* passwd;
const char* dbname;
const char* host;
unsigned int port;
const char* unix_socket;
long unsigned int client_flag;
};
class mmysql {
public:
static mmysql *pInstall; // 连接事例
MYSQL *p;
struct mysqlLogin login;
static mmysql* makeInstall();
// 错误信息
void print_error();
// mysql连接
void mysql_connect(const char* user, const char* passwd, const char* dbname);
// sql 查询
void mysql_querys(const char* query);
// 获取更新行数
long unsigned int mysql_cout();
// 数据读取
void mysql_read();
// 存储读取数据
string **tep;
int row;
int cow;
private:
// 错误号
unsigned int erron;
mmysql();
public:
~mmysql();
};
#endif
mmysql.cpp
/*************************************************************************
> File Name: mysql.cpp
> Author: mouse
> Mail: 2395360675@qq.com
> Created Time: Tue 07 Nov 2017 04:19:27 PM CST
************************************************************************/
#include "mmysql.h"
#include <iostream>
#include <cstring>
using namespace std;
mmysql* mmysql::pInstall = new mmysql();
mmysql::mmysql() {
this->p = mysql_init(NULL);
}
mmysql::~mmysql() {
mysql_close(this->p);
}
mmysql* mmysql::makeInstall() {
return pInstall;
}
void mmysql::mysql_connect(const char* user, const char* passwd, const char* dbname) {
if(mysql_real_connect(this->p, "localhost", user, passwd, dbname, 0, NULL, 0)) {
// cout << "连接成功" << endl;
// 设置编码
mysql_query(p, "set names utf8");
} else {
this->erron = 1;
}
}
void mmysql::print_error() {
switch(this->erron) {
case 1:
cout << "连接失败" << mysql_error(this->p) << endl;
break;
case 2:
cout << "查询失败" << mysql_error(this->p) << endl;
break;
default:
cout << "错误信息不存在" << endl;
break;
}
}
void mmysql::mysql_querys(const char* query) {
if(mysql_query(this->p, query)) {
this->erron = 2;
}
}
long unsigned int mmysql::mysql_cout() {
return mysql_affected_rows(this->p);
}
void mmysql::mysql_read() {
// 释放数据
if(this->row != NULL) {
for(int i = 0; i < this->cow; i++ ) {
delete[] this->tep[i];
}
delete[] this->tep;
}
MYSQL_RES *result = mysql_store_result(this->p);
MYSQL_ROW row;
int cow_lenth = result->field_count;
int row_lenth = result->row_count;
// 数据存储
this->tep = new string* [row_lenth];
for( int i=0; i<row_lenth; i++) {
this->tep[i] = new string[cow_lenth];
}
// 行
this->row = row_lenth;
// 列
this->cow = cow_lenth;
for (int i = 0; i < row_lenth; i++) {
row = mysql_fetch_row(result);
for( int j = 0; j < cow_lenth; j++ ) {
this->tep[i][j] = row[j];
}
}
// 释放数据
mysql_free_result(result);
}
int main() {
mmysql *pInstall = mmysql::makeInstall();
pInstall->mysql_connect("root", "zjf199694", "test");
pInstall->mysql_querys("select * from test");
pInstall->mysql_read();
pInstall->mysql_querys("select * from test");
for(int i = 0; i < pInstall->row; i++) {
for(int j = 0; j < pInstall->cow; j++) {
cout << pInstall->tep[i][j] << "\t";
}
cout << endl;
}
pInstall->mysql_read();
for(int i = 0; i < pInstall->row; i++) {
for(int j = 0; j < pInstall->cow; j++) {
cout << pInstall->tep[i][j] << "\t";
}
cout << endl;
}
cout << "aaa" << endl;
return 0;
/* MYSQL *p;
p = mysql_init(NULL);
if(!mysql_real_connect(p, "localhost", "root", "zjf199694", "test", 0, NULL, 0)) {
cout << "无法连接数据库" << mysql_error(p);
return -1;
}
mysql_query(p, "set names utf8");
if(mysql_query(p, "select * from test")) {
cout << "查询失败" << mysql_error(p);
return -1;
}
MYSQL_RES* result = mysql_store_result(p);
cout << result->field_count << endl;
cout << result->row_count << endl;
MYSQL_ROW row;
while(row = mysql_fetch_row(result)) {
cout << row[0] << endl;
}
mysql_free_result(result);
mysql_close(p);
cout << "aaa" << endl;
return 0;
*/
}
运行结果: