Clion 连接 MySQL 配置教程 C++操作数据库程序 附数据库sql语句
一、将MySQL目录下的 libmysql.dll 和 libmysql.lib 复制到工程目录
粘贴至工程目录中的 cmake-build-debug
文件夹中
libmysql.dll
和 libmysql.lib
位于 MySQL根目录下的 lib
文件夹中
以我的为例 C:\Program Files\MySQL\MySQL Server 8.0\lib
二、配置 CMakeLists.txt
1.引入库
cmake_minimum_required(VERSION 3.19) ##与本次配置无关
project(Clion) ##与本次配置无关
set(CMAKE_CXX_STANDARD 11) ##与本次配置无关
include_directories(C:\\Program\ Files\\MySQL\\include) ##本机MySQL下的include路径
link_directories(C:\\Program\ Files\\MySQL\\lib) ##本机MySQL下的lib路径
link_libraries(libmysql) ##这个这样填就行
add_executable(Clion main.cpp) ## Clion改为自己的工程文件 main.cpp改为自己的C++程序名称
target_link_libraries(Clion libmysql) ##Clion改为自己的工程文件
2.着重强调!!!
include_directories(C:\\Program\ Files\\MySQL\\include) ##本机MySQL下的include路径
link_directories(C:\\Program\ Files\\MySQL\\lib) ##本机MySQL下的lib路径
此两条引入路径时所有的 \ 需改为 \\
且所有包含空格的位置在空格前加上 \
构成转义字符 '\ '
三、如在连接时报错的解决方案
若报错 Authentication plugin 'caching_sha2_password' cannot be loaded
是因为MySQL 8.0的版本中加密规则由mysql_native_password
变为了caching_sha2_password
导致的
打开MySQL命令行
查看 uesr
host
以及加密方式:
select user,host,plugin from user;
修改加密规则:
ALTER USER 'root' @ 'localhost' IDENTIFIED BY '设定的root密码' PASSWORD EXPIRE NEVER;
此处 host
若为 %
将 localhost
改为 %
ALTER USER 'root' @ 'localhost' IDENTIFIED WITH mysql_native_password BY '设定的root密码';
再刷新权限 FLUSH PRIVILEGES;
操作数据库程序
#include <bits/stdc++.h>
#include <mysql.h>
using namespace std;
int tp;
string in1,in2,in3,in4,in5,in6;
MYSQL mysql;
MYSQL_RES *res;//该结构代表返回行的一个查询结果集
MYSQL_ROW column;//一个行数据的类型安全(type-safe)的表示
bool ConnectMysql()
{
mysql_init(&mysql);//初始化mysql
if (!(mysql_real_connect(&mysql,"localhost","root","0122","csgl",3306,NULL, 0)))
{
cout << "Error connecting to database:" + (string) mysql_error(&mysql) << endl;
return false;
}
else
{
puts("Connected...");
return true;
}
}
bool updateData(string sql)
{
mysql_query(&mysql, "set names gbk");//设置编码格式 避免中文乱码
// 执行SQL语句
// 0 执行成功
// 1 执行失败
if (mysql_query(&mysql, sql.c_str()))//mysql_query第二个参数只接受const char* 需进行类型转化
{
cout << "Update failed ( " + (string) mysql_error(&mysql) + " )" << endl;
return false;
}
else
{
puts("Update success...");
return true;
}
}
void outMysql()
{
//打印数据行数
cout << "number of dataline returned: " << mysql_affected_rows(&mysql) << endl;
char *field[32];//字段名
int num = mysql_num_fields(res);//获取列数
for (int i = 0; i < num; ++i)
{
field[i] = mysql_fetch_field(res)->name;//获取字段名
}
for (int i = 0; i < num; ++i)
{
cout << (string) field[i] << " ";
}
puts("");
while (column = mysql_fetch_row(res))//获取一行数据
{
for (int i = 0; i < num; ++i)
{
cout << column[i] << " ";
}
cout << endl;
}
}
bool QueryDatabase(string sql)
{
mysql_query(&mysql, "set names gbk");
// 执行SQL语句
// 0 执行成功
// 1 执行失败
if (mysql_query(&mysql, sql.c_str()))
{
cout << "Query failed ( " + (string) mysql_error(&mysql) + " )" << endl;
return false;
}
else
{
cout << "query success..." << endl;
}
//获得结果集 MYSQL_RES *res;
if (!(res = mysql_store_result(&mysql)))
{
cout << "Couldn't get result from " + (string) mysql_error(&mysql) << endl;
return false;
}
outMysql();//输出结果
return true;
}
void FreeConnect()
{
mysql_free_result(res);
mysql_close(&mysql);
}
int main()
{
ConnectMysql();
puts("-------Please select operation-------");
puts("1:insert");
puts("2:delete");
puts("3:update");
puts("4:select");
puts("5:exit");
while(~scanf("%d",&tp))
{
if(tp==1)
{
puts("-------Please select sheet insert-------");
puts("1:customer");
puts("2:goods");
puts("3:supplier");
puts("4:transactionn");
scanf("%d",&tp);
if(tp==1)
{
puts("-------Please cin >> tel,name,address,vip?-------");
cin>>in1>>in2>>in3>>in4;
updateData("insert into customer (C_tel,C_name,C_address,C_vip) values('"+in1+"','"+in2+"','"+in3+"','"+in4+"')");
}
else if(tp==2)
{
puts("-------Please cin >> number,name,category,specification,unitPrice-------");
cin>>in1>>in2>>in3>>in4>>in5;
updateData("insert into goods (G_number,G_name,G_category,G_specification,G_unitPrice) values('"+in1+"','"+in2+"','"+in3+"','"+in4+"',"+in5+")");
}
else if(tp==3)
{
puts("-------Please cin >> number,name,linkman-------");
cin>>in1>>in2>>in3;
updateData("insert into supplier (S_number,S_name,S_linkman) values('"+in1+"','"+in2+"','"+in3+"')");
}
else if(tp==4)
{
puts("-------Please cin >> number,name,category,specification,unitPrice-------");
cin>>in1>>in2>>in3>>in4;
updateData("insert into transactionn (T_number,T_tel,T_quantity,T_sumMoney) values('"+in1+"','"+in2+"',"+in3+","+in4+")");
}
else
{
puts("not exist!");
continue;
}
}
else if(tp==2)
{
puts("-------Please select sheet delete-------");
puts("1:customer");
puts("2:goods");
puts("3:supplier");
puts("4:transactionn");
scanf("%d",&tp);
if(tp==1)
{
puts("-------Please cin >> tel-------");
cin>>in1;
updateData("delete from customer where C_tel='"+in1+"'");
}
else if(tp==2)
{
puts("-------Please cin >> number-------");
cin>>in1;
updateData("delete from goods where G_number='"+in1+"'");
}
else if(tp==3)
{
puts("-------Please cin >> number-------");
cin>>in1;
updateData("delete from supplier where S_number='"+in1+"'");
}
else if(tp==4)
{
puts("-------Please cin >> number,tel-------");
cin>>in1>>in2;
updateData("delete from transactionn where T_number='"+in1+"' and T_tel='"+in2+"'");
}
else
{
puts("not exist!");
continue;
}
}
else if(tp==3)
{
puts("-------Please select sheet update-------");
puts("1:customer");
puts("2:goods");
puts("3:supplier");
puts("4:transactionn");
scanf("%d",&tp);
if(tp==1)
{
puts("-------Please cin >> tel,name,address,vip? >> tel-------");
cin>>in1>>in2>>in3>>in4>>in5;
updateData("update customer set C_tel='"+in1+"',C_name='"+in2+"',C_address='"+in3+"',C_vip='"+in4+"' where C_tel='"+in5+"'");
}
else if(tp==2)
{
puts("-------Please cin >> number,name,category,specification,unitPrice >> number-------");
cin>>in1>>in2>>in3>>in4>>in5>>in6;
updateData("update goods set G_number='"+in1+"',G_name='"+in2+"',G_category='"+in3+"',G_specification='"+in4+"',G_unitPrice="+in5+" where G_number='"+in6+"'");
}
else if(tp==3)
{
puts("-------Please cin >> number,name,linkman >> number-------");
cin>>in1>>in2>>in3>>in4;
updateData("update supplier set S_number='"+in1+"',S_name='"+in2+"',S_linkman='"+in3+"' where S_number='"+in4+"'");
}
else if(tp==4)
{
puts("-------Please cin >> number,name,category,specification,unitPrice >> number,tel-------");
cin>>in1>>in2>>in3>>in4>>in5>>in6;
updateData("update transactionn set T_number='"+in1+"',T_tel='"+in2+"',T_quantity="+in3+",T_sumMoney="+in4+" where T_number='"+in5+"' and T_tel='"+in6+"'");
}
else
{
puts("not exist!");
continue;
}
}
else if(tp==4)
{
puts("-------Please select sheet-------");
puts("1:customer");
puts("2:goods");
puts("3:supplier");
puts("4:transactionn");
scanf("%d",&tp);
if(tp==1)QueryDatabase("select * from customer");
else if(tp==2)QueryDatabase("select * from goods");
else if(tp==3)QueryDatabase("select * from supplier");
else if(tp==4)QueryDatabase("select * from transactionn");
else
{
puts("not exist!");
continue;
}
}
else if(tp==5)
{
puts("Bye");
break;
}
puts("-------Please select operation-------");
puts("1:insert");
puts("2:delete");
puts("3:update");
puts("4:select");
puts("5:exit");
}
FreeConnect();
return 0;
}
附上程序对应数据库的 sql 语句
CREATE DATABASE CSGL;
USE CSGL;
CREATE TABLE Customer( /*顾客*/
C_tel CHAR(11) PRIMARY KEY NOT NULL, /*电话*/
C_name CHAR(15) NOT NULL, /*姓名*/
C_address CHAR(100) NOT NULL, /*地址*/
C_vip CHAR(8) NOT NULL/*是or否*/
);
CREATE TABLE Supplier( /*供货商*/
S_number CHAR(15) PRIMARY KEY NOT NULL, /*商品编号*/
S_name CHAR(30) NOT NULL, /*商品编号*/
S_linkman CHAR(15) NOT NULL /*联系人*/
);
CREATE TABLE Goods( /*商品*/
G_number CHAR(15) PRIMARY KEY NOT NULL, /*商品编号*/
G_name CHAR(30) NOT NULL, /*商品名称*/
G_category CHAR(30) NOT NULL, /*类别*/
G_specification CHAR(10) NOT NULL, /*商品规格*/
G_unitPrice int /*单价*/
);
CREATE TABLE Transactionn( /*交易记录*/
T_number CHAR(15) , /*商品编号*/
T_tel CHAR(11) , /*顾客电话*/
PRIMARY KEY(T_number,T_tel),
T_quantity int, /*数量*/
T_sumMoney int, /*应收总金额*/
FOREIGN KEY(T_number)REFERENCES Supplier(S_number),
FOREIGN KEY(T_tel)REFERENCES Customer(C_tel)
);