当前位置:   article > 正文

VsCode + CMake构建项目 C/C++连接Mysql数据库 | 数据库增删改查C++封装 | 信息管理系统通用代码 ---- 课程笔记_vscode中c++链接数据库

vscode中c++链接数据库

这个是B站Up主:程序员程子青的视频 

C++封装Mysql增删改查操作_哔哩哔哩_bilibiliicon-default.png?t=N7T8https://www.bilibili.com/video/BV1m24y1a79o/?p=6&spm_id_from=pageDriver&vd_source=a934d7fc6f47698a29dac90a922ba5a3安装mysql:mysql 下载和安装和修改MYSQL8.0 数据库存储文件的路径-CSDN博客

创建数据库和表:

  1. C:\Users\heheda>mysql -u heheda -p
  2. mysql> create database test;
  3. mysql> show databases;
  4. mysql> use test;
  5. mysql> create table student(stuId int not null auto_increment primary key,stuName varchar(255) not null,className varchar(255) not null);
  6. mysql> show tables;
  7. mysql> insert into student values(3,'杰瑞','终极三班');
  8. Query OK, 1 row affected (0.00 sec)

参考这篇文章:windows下使用vscode原生态配置c++链接mysql数据库:windows下使用vscode原生态配置c++链接mysql数据库_vscode 链接 lib库-CSDN博客

  • mysql下的include文件夹直接拷贝到项目目录下【或者只拷贝include中的mysql.h文件】方便引用mysql.h头文件
  •  拷贝libmysql.dll 、libmysql.lib、mysqlclient.lib文件直接放在工程目录下因为这里可执行文件在其所在目录下直接寻找动态链接源文件
  •  当cmake构建好项目之后,我们可以把libmysql.dll 、libmysql.lib文件直接放在bin目录下

  • StudentManager.h
  1. #pragma once
  2. #include <mysql.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. using namespace std;
  7. typedef struct student {
  8. int stuId;
  9. string stuName;
  10. string className;
  11. }Student;
  12. class StudentManager {
  13. private:
  14. StudentManager();
  15. ~StudentManager();
  16. public:
  17. static StudentManager* GetInstance() { //单例修改
  18. static StudentManager StudentManager;
  19. return &StudentManager;
  20. }
  21. public:
  22. // 增上改查
  23. bool insertStu(Student& stu);
  24. bool updateStu(Student& stu);
  25. bool deleteStu(int stuId);
  26. vector<Student> queryStu(string condition = "");
  27. private:
  28. MYSQL* conn;
  29. const char* host = "127.0.0.1";
  30. const char* user = "heheda";
  31. const char* pwd = "123456";
  32. const char* dbName = "test";
  33. const unsigned short port = 3306;
  34. const char* tableName = "student";
  35. };
  • StudentManager.cpp
  1. #include "StudentManager.h"
  2. StudentManager::StudentManager() {
  3. conn = mysql_init(NULL);
  4. // 设置字符编码
  5. mysql_options(conn, MYSQL_SET_CHARSET_NAME, "GBK");
  6. if(!mysql_real_connect(conn,host,user,pwd,dbName,port,NULL,0)) {
  7. std::cout<<"Failed to connect"<<std::endl;
  8. exit(1);
  9. }
  10. }
  11. StudentManager::~StudentManager() {
  12. mysql_close(conn);
  13. }
  14. bool StudentManager::insertStu(Student &stu) {
  15. char sql[1024];
  16. sprintf(sql,"insert into student (stuId,stuName,className) values(%d,'%s','%s')",
  17. stu.stuId,stu.stuName.c_str(),stu.className.c_str());
  18. // mysql_query成功返回0,失败返回非0
  19. if(mysql_query(conn,sql)) {
  20. fprintf(stderr,"Failed to insert data into database!!!Error:%s\n",
  21. mysql_error(conn));
  22. return false;
  23. }
  24. return true;
  25. }
  26. // c_str():生成一个const char*指针,指向以空字符终止的数组
  27. bool StudentManager::updateStu(Student &stu) {
  28. char sql[1024];
  29. sprintf(sql,"UPDATE student SET stuName = '%s',className = '%s'"
  30. "where stuId = %d",stu.stuName.c_str(),stu.className.c_str(),stu.stuId);
  31. if(mysql_query(conn,sql)) {
  32. fprintf(stderr,"Failed to update data!!!Error:%s\n",mysql_error(conn));
  33. return false;
  34. }
  35. return true;
  36. }
  37. bool StudentManager::deleteStu(int stuId) {
  38. char sql[1024];
  39. sprintf(sql,"DELETE FROM student WHERE stuId = '%d'",stuId);
  40. if(mysql_query(conn,sql)) {
  41. fprintf(stderr,"Failed to delete data!!!Error:%s\n",mysql_error(conn));
  42. return false;
  43. }
  44. return true;
  45. }
  46. vector<Student> StudentManager::queryStu(string condition) {
  47. vector<Student> stuList;
  48. char sql[1024];
  49. sprintf(sql,"SELECT * FROM student %s",condition.c_str());
  50. if(mysql_query(conn,sql)) {
  51. fprintf(stderr,"Failed to select data!!!Error:%s\n",mysql_error(conn));
  52. return {};
  53. }
  54. MYSQL_RES* res = mysql_store_result(conn);
  55. MYSQL_ROW row;
  56. while((row = mysql_fetch_row(res))) {
  57. Student stu;
  58. stu.stuId = atoi(row[0]);
  59. stu.stuName = row[1];
  60. stu.className = row[2];
  61. stuList.push_back(stu);
  62. }
  63. return stuList;
  64. }
  • CMakeLists.txt
  1. cmake_minimum_required(VERSION 3.10)
  2. project(test)
  3. include_directories(${PROJECT_SOURCE_DIR}/include)
  4. set(StudentManager ${PROJECT_SOURCE_DIR}/StudentManager)
  5. include_directories(${StudentManager}/include)
  6. aux_source_directory(${StudentManager}/src StudentManagerSrc)
  7. link_directories(${PROJECT_SOURCE_DIR}/lib)
  8. add_executable(app test.cpp ${StudentManagerSrc})
  9. target_link_libraries(app mysql)
  10. # 指定输出的路径
  11. set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
  12. set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给 EXECUTABLE_OUTPUT_PATH 变量
  • test.cpp
  1. #include "StudentManager.h"
  2. #include <mysql.h>
  3. int main() {
  4. Student stu{3,"杰瑞","猫鼠一班"};
  5. // StudentManager::GetInstance()->insertStu(stu);
  6. // StudentManager::GetInstance()->deleteStu(3);
  7. StudentManager::GetInstance()->updateStu(stu);
  8. char condition[1024];
  9. sprintf(condition,"where className = '%s'","终极一班");
  10. // sprintf(condition,"where stuId = %d",2);
  11. // string condition = string();
  12. vector<Student> ret= StudentManager::GetInstance()->queryStu(condition);
  13. for(auto& it:ret){
  14. std::cout<<"打印: ";
  15. cout<<it.stuId<<" "<<it.stuName<<" "<<it.className<<endl;
  16. }
  17. std::cout<<"I am heheda!"<<std::endl;
  18. return 0;
  19. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/832194
推荐阅读
相关标签
  

闽ICP备14008679号