第一步:先在Linux中安装MySQL
第二步:在安装连接器,中端命令
sudo apt-get install libmysqlclient-dev
第三步写一个操作MySQL的c++代码保存退出
#include
#include //所需头文件
int main()
{
MYSQL *conn_prt; //创造一个MYSQL句柄
conn_prt = mysql_init(NULL); //初始化MYSQL句柄
if(!conn_prt) //若初始化句柄失败
{
printf("connect mysql failed");
return -1;
}
/*尝试与mysql数据库连接*/
if(!mysql_real_connect(conn_prt,"localhost","root"
,"123456","test",0,NULL,0))
{
printf("failed to coonect mysql:%s\n",mysql_error(conn_prt));
}
else
{
printf("connect success...........\n");
}
/*关闭与Mysql数据库的连接,并释放相应内存*/
mysql_close(conn_prt);
return 0;
}
第四步编译代码在终端输入回车然后就ok了
g++ -o mysql mysql.cpp -I/usr/include/mysql/ -l mysqlclient
附加自己用c++封装好的初始化mysql
#include
#include //所需头文件
#include
#include
using namespace std;
class MySQL
{
private:
MYSQL *MYSQLJB;//mysql句柄
public:
void chuanjiangmysqljubing();//创建MySQL句柄 --句柄要是不符合if判断那就是创建句柄失败--
MYSQL *fanghuimysqljubingleixing();//返回创建的MySQL句柄 --句柄要是不符合if判断那就是创建句柄失败--
int yushujukujianlilianjie(MYSQL *JB, const char *ip, const char *yhdrid,const char *yhdrmm,const char *sjkimz,int dk,const char *tjz, int flagxx );//它的第一个参数句柄,第二个为ip地址
//第三个为登入用户名id,第四个为登入用户名登入用户密码,第五个为数据库名字,第六个为端口,第七个为套接字,第八个为flagxx --如果成功if能判断,失败if不能判断
void guanbishujukusifanneicun();//释放数据库
};
//创建MySQL句柄
void MySQL::chuanjiangmysqljubing()
{
this->MYSQLJB= mysql_init(NULL);
}
//返回创建的MySQL句柄
MYSQL * MySQL::fanghuimysqljubingleixing()
{
return this->MYSQLJB;
}
//它的第一个参数句柄,第二个为ip地址
//第三个为登入用户名id,第四个为登入用户名登入用户密码,第五个为数据库名字,第六个为端口,第七个为套接字,第八个为flagxx --如果成功if能判断,失败if不能判断
int MySQL::yushujukujianlilianjie(MYSQL *JB, const char *ip, const char *yhdrid,const char *yhdrmm,const char *sjkmz,int dk,const char *tjz, int flagxx )
{
if(!mysql_real_connect(JB,ip,yhdrid ,yhdrmm,sjkmz,dk,tjz,flagxx))
{
return 0;
}
else
{
return 1;
}
}
//释放数据库内存
void MySQL::guanbishujukusifanneicun()
{
mysql_close(this->MYSQLJB);
}
int main()
{
//创建一个MySQL类
MySQL A;
A.chuanjiangmysqljubing();//穿建句柄
MYSQL *conn_prt= A.fanghuimysqljubingleixing() ; //获取一个MYSQL句柄
if(!A.yushujukujianlilianjie(conn_prt,"localhost","root","密码","操作库名字",0,NULL,0))
{
cout<<"连接失败";
}
mysql_close(conn_prt);
/*
* conn_prt = mysql_init(NULL); //初始化MYSQL句柄
if(!conn_prt) //若初始化句柄失败 { printf("connect mysql failed");
{
return -1; }
/*尝试与mysql数据库连接*/
/*
if(!mysql_real_connect(conn_prt,"localhost","root"
,"IfWinterComes2019.","javazy",0,NULL,0)) {
printf("failed to coonect mysql:%s\n",mysql_error(conn_prt)); }
else { printf("connect success...........\n"); }
/*关闭与Mysql数据库的连接,并释放相应内存*/
/*
mysql_close(conn_prt);
*/
return 0;
}
Linux操作MySQL实现增删改查
#include
#include
#include
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",
"密码",
"操作的库名字",
0,
NULL,
0)) {
printf("mysql_real_connect(): %s\n", mysql_error(&mysql));
return -1;
}
printf("1. Connected MySQL successful! \n");
//执行插入请求
query_str = "insert into tb_users values (12345, 'justtest', '2015-5-5')";
rc = mysql_real_query(&mysql, query_str, strlen(query_str));
if (0 != rc) {
printf("mysql_real_query(): %s\n", mysql_error(&mysql));
return -1;
}
//执行删除请求
query_str = "delete from tb_users where userid=10006";
rc = mysql_real_query(&mysql, query_str, strlen(query_str));
if (0 != rc) {
printf("mysql_real_query(): %s\n", mysql_error(&mysql));
return -1;
}
//执行修改请求
string sql ="update yonhubiao set huiyuang = '0' where zhanghao = '007' ";
query_str = sql.c_str();
rc = mysql_real_query(&mysql, query_str, strlen(query_str));
if (0 != rc) {
printf("mysql_real_query(): %s\n", mysql_error(&mysql));
return -1;
}
//然后查询插入删除之后的数据
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_free_result(res);
mysql_close(&mysql);
return 0;
}