首页 > 数据库 >C++增删改查+MySQL

C++增删改查+MySQL

时间:2023-06-17 21:12:29浏览次数:41  
标签:StudentManager MySQL 改查 C++ stu student mysql id con

右键项目属性

 点击编辑

 选择mysql安装目录的include文件夹

 包含了头文件之后包含库文件

 点击编辑

在mysql安装路径下面选择

 

 选择编辑之后添加

看这个文件下面有没有这个输入的文件依赖

 有就代表成功连接数据库

 创建数据库:

create database student_manager;

使用数据库:

use student_manager

创建表:

create table student(student_id int not null auto_increment primary key,student_name varchar(255) not null,class_id varchar(255) not null);

插入数据:

insert into student values(1000,'张三','软件工程-班');

 详细环境配置:

https://blog.csdn.net/zapachep/article/details/121547648

 

 复制到当前文件下:

 源代码:测试一下连接步骤

#include<mysql.h>
#include<iostream>
#include<string>
using namespace std;
const char* host = "127.0.0.1";
const char* user = "root";
const char* pw = "mxz101305";
const char* database_name = "student_manager";
const int port = 3306;
typedef struct Student {
int student_id;
string student_name;
string class_id;
}Student;
int main() {
//初始化,连接mysql,
MYSQL* con = mysql_init(NULL);
//设置编码,以防止乱码
mysql_options(con, MYSQL_SET_CHARSET_NAME, "GBK");
//连接
if (!mysql_real_connect(con, host, user, pw, database_name, port, NULL, 0)) {
fprintf(stderr, "数据库连接错误:%s\n", mysql_error(con));
return -1;
}
Student stu{ 1111,"吴彦祖", "软件三班" };
char sql[1024];
sprintf_s(sql, "insert into student(student_id,student_name,class_id) values(%d,'%s','%s')", stu.student_id, stu.student_name.c_str(), stu.class_id.c_str());

//失败为1,成功为0
if (mysql_query(con, sql)) {
fprintf(stderr, "操作失败:%s\n", mysql_error(con));
return -1;
}

//关闭连接
mysql_close(con);

return 0;

}

完整的数据库增删改查操作:

头文件

 

#pragma once
#include<mysql.h>
#include<iostream>
#include<string>
#include<vector>
using namespace std;

typedef struct Student {
int student_id;
string student_name;
string class_id;
}Student;
class StudentManager
{
StudentManager();
~StudentManager();
public://单例模式,限制只有一个连接
static StudentManager* GetInstance() {
static StudentManager studentManager;
return &studentManager;
}
public:
bool insert_student(Student& t);
bool update_student(Student& t);
bool delete_student(int student_id);
vector<Student> get_student(string condition = "");
private:
MYSQL* con;
const char* host = "127.0.0.1";
const char* user = "root";
const char* pw = "mxz101305";
const char* database_name = "student_manager";
const int port = 3306;


};

 

 

#include "StudentManager.h"

StudentManager::StudentManager()
{
con = mysql_init(NULL);
//设置编码,以防止乱码
mysql_options(con, MYSQL_SET_CHARSET_NAME, "GBK");
//连接
if (!mysql_real_connect(con, host, user, pw, database_name, port, NULL, 0)) {
fprintf(stderr, "数据库连接错误:%s\n", mysql_error(con));
exit(1);
}
}

StudentManager::~StudentManager()
{
mysql_close(con);
}

bool StudentManager::insert_student(Student& stu)
{


char sql[1024];
sprintf_s(sql, "insert into student(student_id,student_name,class_id) values(%d,'%s','%s')", stu.student_id, stu.student_name.c_str(), stu.class_id.c_str());
if (mysql_query(con, sql)) {
fprintf(stderr, "增加操作失败:%s\n", mysql_error(con));
return false;

}
return true;
}

bool StudentManager::update_student(Student& stu)
{
char sql[1024];
sprintf_s(sql, "UPDATE student SET student_name='%s',class_id='%s'""where student_id=%d",
stu.student_name.c_str(), stu.class_id.c_str(), stu.student_id);
if (mysql_query(con, sql)) {
fprintf(stderr, "更新操作失败:%s\n", mysql_error(con));
return false;

}
return true;

}

bool StudentManager::delete_student(int student_id)
{
char sql[1024];
sprintf_s(sql, "DELETE FROM student where student_id=%d",student_id);
if (mysql_query(con, sql)) {
fprintf(stderr, "删除操作失败:%s\n", mysql_error(con));
return false;

}
return true;

}

vector<Student> StudentManager::get_student(string condition)
{
vector<Student> stuList;
char sql[1024];
sprintf_s(sql, "SELECT * FROM student %s",condition.c_str());
if (mysql_query(con, sql)) {
fprintf(stderr, "查询操作失败:%s\n", mysql_error(con));
return {};

}
MYSQL_RES* res = mysql_store_result(con);
MYSQL_ROW row;
while ((row = mysql_fetch_row(res))) {
Student stu;
stu.student_id = atoi(row[0]);
stu.student_name = row[1];
stu.class_id = row[2];
stuList.push_back(stu);
}
return stuList;
}

验证代码效果:

 

 

#include "StudentManager.h"
int main() {

/*增删改操作
Student stu{ 11114, "海燕12", "数学3班" };
StudentManager::GetInstance()->delete_student(11114);*/

vector<Student> ret = StudentManager::GetInstance()->get_student();
for (auto& t : ret) {
cout << t.student_id << "," << t.student_name << "," << t.class_id << endl;
}
return 0;
}

标签:StudentManager,MySQL,改查,C++,stu,student,mysql,id,con
From: https://www.cnblogs.com/moxiaozhi/p/17488249.html

相关文章

  • mysql5.7密码策略说明
    一、mysql5.7在创建用户设置密码时提示“ERROR1819(HY000):Yourpassworddoesnotsatisfythecurrentpolicyrequirements”createuser'tom'@localhostidentifiedby'123456';ERROR1819(HY000):Yourpassworddoesnotsatisfythecurrentpolicyrequi......
  • MySQL错误类型1030
     该错误类型一般为磁盘内存空间不足。常规情况下清除备份文件即可。引用:mysql出现1030Goterror28fromstorageengine解决方法_风火程序员的博客-CSDN博客......
  • Mysql 触发器smysql
    触发器1.创建--trigger_name:触发器的名称--BEFORE或AFTER用于指定触发时机--INSERT、UPDATE、DELETE用于指定触发事件--table_name为触发器所关联的表名--FOREACHROW表示针对每一行数据的变化而触发--trigger_body为触发器的执行语句CREATETRIGGERtrigg......
  • GESP-C++-4
    GESPC++四级样题卷在C++中,指针变量的大小(单位:字节)是()A.2B.4C.8D.与编译器有关答案:D以下哪个选项能正确定义一个二维数组()A.inta[][];B.charb[][4];C.doublec[3][];D.boold[3][4];答案:D在C++中,以下哪种方式不能用于向函数传递参数()A.值传递B.......
  • GESP-C++-3
    GESPC++三级样题卷下列关于负数的原码、反码、补码的描述中,正确的是()A.原码和反码互为按位取反(符号位除外),补码为反码加1B.原码和反码互为按位取反(符号位除外),补码为原码加1C.反码和补码互为按位取反(符号位除外),原码为反码加1D.补码和原码互为按位取反(符号位除外),反......
  • mysql四舍五入函数取两位小数
    MySQL四舍五入函数ROUND(x)ROUND(x)函数返回最接近于参数x的整数,对x值进行四舍五入。实例:使用ROUND(x)函数对操作数进行四舍五入操作。SQL语句如下:mysql>SELECTROUND(-2.34),ROUND(-4.56),ROUND(2.34),ROUND(4.56);ROUND(x)函数的执行结果如下图所示:上图中代码执行的结果显示,进行......
  • 【C++】值初始化
    如果自己建一个类,例如:classA{public:A(){cout<<"A"<<endl;}inti;};在main主函数中如下的两行代码:A*pa1=newA;A*pa2=newA();效果一样,都是调用A的构造函数,也就是说,自己定义的类,在new该类的对象时,所谓的值初始化是没有意义的。所......
  • GESP-C++-2
    GESPC++二级样题卷人们在使用计算机时所提到的Windows通常指的是()。A.操作系统B.多人游戏C.上市公司D.家居用具答案:A万维网WWW中存储了海量的数据资源,这里用于传输控制的协议是()。A.URLB.SMTPC.HTTPD.HTML答案:C下列关于C++语言的叙述,不正确的是()......
  • C++通讯录管理系统[2023-06-17]
    C++通讯录管理系统[2023-06-17]通讯录管理系统手机通讯录中的联系人的信息既可以存储在手机中,也可以存储在手机卡中,也可以同时存储在两个位置上(每个位置上的存储容量为1000,即手机卡中或手机上最多只能存储1000个联系人)。存储在手机中的联系人的信息只包含用户名和电话号码两项信......
  • GESP-C++-1
    GESPC++一级样题卷一、单选题(每题2分,共30分)人们在使用计算机时所提到的Windows通常指的是()。A.操作系统B.多人游戏C.上市公司D.家居用具答案:A计算机领域的图灵奖为了纪念()科学家图灵。A.英国B.德国C.瑞典D.法国答案:A下列关于C++语言的叙述,不......