C/C++数据库编程

文章目录

  • 0. Mysql安装与开发环境配置
  • 1. win10 Navicat 连接虚拟机的MySQL需要关闭防火墙
  • 2. 由于找不到libmysql.dIl, 无法继续执行代码。重新安装程序可能会解决此问题。
  • 3. 测试连接数据库,并插入数据
  • 4. C++封装MySQL增删改查操作

0. Mysql安装与开发环境配置

MySQL安装_win10(超详细)

C/C++访问MySQL数据库

1. win10 Navicat 连接虚拟机的MySQL需要关闭防火墙

  1. 查看防火墙端口开放的情况:
service firewalld status
  1. 关闭防火墙:
systemctl stop firewalld

2. 由于找不到libmysql.dIl, 无法继续执行代码。重新安装程序可能会解决此问题。

D:\MySQL\mysql-8.0.33-winx64\lib目录下的libmysql.dll拷贝到E:\Code\VS2022\student_manager\student_manager

3. 测试连接数据库,并插入数据

#include <iostream>
#include <mysql.h>
#include <string>using namespace std;const char* host = "127.0.0.1";
const char* user = "root";
const char* pw = "111111";
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* 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, "Failed to connect to database : Error:%s\n", mysql_errno(con));return -1;}Student stu = { 1001, "吴彦祖", "计算机2班" };char sql[256];sprintf_s(sql, "insert into students(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, "Failed to insert data : Error:%s\n", mysql_errno(con));return -1;}mysql_close(con);return 0;
}

4. C++封装MySQL增删改查操作

// StudentManager.h
#ifndef STUDENTMANAGER_H
#define STUDENTMANAGER_H#include <mysql.h>
#include <string>
#include <vector>
#include <iostream>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& stu);bool update_student(Student& stu);bool delete_student(int student_id);vector<Student> get_students(string condition = "");private:MYSQL* con;const char* host = "127.0.0.1";const char* user = "root";const char* pw = "111111";const char* database_name = "student_manager";const int port = 3306;
};#endif // STUDENTMANAGER_H
// StudentManager.cpp
#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, "Failed to connect to database : Error:%s\n", mysql_errno(con));exit(1);}
}StudentManager::~StudentManager() {mysql_close(con);
}bool StudentManager::insert_student(Student& stu) {char sql[256];sprintf_s(sql, "INSERT INTO students(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, "Failed to insert data : Error:%s\n", mysql_errno(con));return false;}return true;
}bool StudentManager::update_student(Student& stu) {char sql[256];sprintf_s(sql, "UPDATE students 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, "Failed to update data : Error:%s\n", mysql_errno(con));return false;}return true;
}bool StudentManager::delete_student(int student_id) {char sql[256];sprintf_s(sql, "DELETE FROM students WHERE student_id = %d", student_id);if (mysql_query(con, sql)) {fprintf(stderr, "Failed to delete data : Error:%s\n", mysql_errno(con));return false;}return true;
}vector<Student> StudentManager::get_students(string condition) {vector<Student> stuList;char sql[256];sprintf_s(sql, "SELECT * FROM students %s", condition.c_str());if (mysql_query(con, sql)) {fprintf(stderr, "Failed to select data : Error:%s\n", mysql_errno(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.emplace_back(stu);}return stuList;
}
// main.cpp
#include "StudentManager.h"int main() {Student stu{999, "彭于晏", "网工1班"};StudentManager::GetInstance()->insert_student(stu);Student stu{999, "彭于晏", "网工3班" };StudentManager::GetInstance()->update_student(stu);StudentManager::GetInstance()->delete_student(1000);vector<Student> ret = StudentManager::GetInstance()->get_students();for (auto& t : ret) {cout << t.student_id << " " << t.student_name << " " << t.class_id << endl;}return 0;
}// main.cpp
#include "StudentManager.h"int main() {StudentManager* studentManager = StudentManager::GetInstance();Student stu1{1009, "彭于晏", "网工1班"};studentManager->insert_student(stu1);Student stu2{999, "胡歌", "网工3班" };studentManager->update_student(stu2);studentManager->delete_student(1001);vector<Student> ret = studentManager->get_students();for (auto& t : ret) {cout << t.student_id << " " << t.student_name << " " << t.class_id << endl;}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/51133.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IDEA下SpringBoot指定环境、配置文件启动

1、idea下的SpringBoot启动&#xff1a;指定配置文件 Springboot项目有如下配置文件 主配置文件application.yml&#xff0c; 测试环境&#xff1a;application-test.yml 生产环境&#xff1a;application-pro.yml 开发环境&#xff1a;application-dev.yml 1.1.配置文件…

【FreeRTOS】【STM32】中断详细介绍

文章目录 一、三种优先级的概念辨析1. 先理清楚两个概念&#xff1a;CPU 和 MPU2. Cortex-M3 内核与 STM32F1XX 控制器有什么关系3. 优先级的概念辨析① Cortex-M3 内核和 STM32F1XX 的中断优先级② FreeRTOS 的任务的优先级 二、 Cortex-M3 内核的中断优先级1. 中断编号2. 优先…

Android 系统桌面 App —— Launcher 开发(1)

Android 系统桌面 App —— Launcher 开发&#xff08;1&#xff09; Launcher简介 Launcher就是Android系统的桌面&#xff0c;俗称“HomeScreen”也就是我们开机后看到的第一个App。launcher其实就是一个app&#xff0c;它的作用是显示和管理手机上其他App。目前市场上有很…

.NET CORE Api 上传excel解析并生成错误excel下载

写在前面的话&#xff1a; 【对外承接app API开发、网站建设、系统开发&#xff0c;有偿提供帮助&#xff0c;联系方式于文章最下方 】 因业务调整&#xff0c;不再需要生成错误无excel下载&#xff0c;所以先保存代码&#xff0c;回头再重新编辑 #region Excel校验部分if (f…

VIT Swin Transformer

VIT&#xff1a;https://blog.csdn.net/qq_37541097/article/details/118242600 Swin Transform&#xff1a;https://blog.csdn.net/qq_37541097/article/details/121119988 一、VIT 模型由三个模块组成&#xff1a; Linear Projection of Flattened Patches(Embedding层) Tran…

星际争霸之小霸王之小蜜蜂(六)--让子弹飞

目录 前言 一、添加子弹设置 二、创建子弹 三、创建绘制和移动子弹函数 四、让子弹飞 五、效果 总结 前言 小蜜蜂的基本操作已经完成了&#xff0c;现在开始编写子弹的代码了。 一、添加子弹设置 在我的预想里&#xff0c;我们的小蜜蜂既然是一只猫&#xff0c;那么放出的子弹…

List的流操作结合hutool的最优雅处理,处理前端所需要的参数定制化返回给前端

前言: java用于服务器端是很方便的,特别是对于前后端分离的项目来说.经常需要对接前端写接口,有时候前端要接口必须得一次性查出来数据,那就得处理从数据库查询到的数据了,下面就是如何更加高效更加优雅的处理数据,这里用到了hutool的方法,这篇文章主要就是让大家如何快速的去处…

微信小程序开发教学系列(1)- 开发入门

第一章&#xff1a;微信小程序简介与入门 1.1 简介 微信小程序是一种基于微信平台的应用程序&#xff0c;可以在微信内直接使用&#xff0c;无需下载和安装。它具有小巧、高效、便捷的特点&#xff0c;可以满足用户在微信中获取信息、使用服务的需求。 微信小程序采用前端技…

自定义WEB框架结合Jenkins实现全自动测试

自定义WEB框架结合Jenkins实现全自动测试 allure生成 allure生成 1.allure–纯命令运行 -固定的–稍微记住对应的单词即可。2 安装&#xff0c;2个步骤: 1.下载allure包&#xff0c;然后配置环境变量。 https://github.com/allure-framework/allure2/releases/tag/2.22.4 2.在…

mysql 、sql server 临时表、表变量、

sql server 临时表 、表变量 mysql 临时表 创建临时表 create temporary table 表名 select 字段 [&#xff0c;字段2…&#xff0c;字段n] from 表

leetcode 355 设计推特

用链表存储用户发送的每一个推特&#xff0c;用堆获取最先的10条动态 class Twitter {Map<Integer,Set<Integer>> followMap;//规定最新的放到最后Map<Integer,Tweet> postMap;//优先队列(堆&#xff09;PriorityQueue<Tweet> priorityQueue;int time…

[JavaWeb]【十】web后端开发-SpringBootWeb案例(配置文件)

目录 一、参数配置化 1.1 问题分析 1.2 问题解决&#xff08;application.properties&#xff09; 1.2.1 application.properties 1.2.2 AliOSSUtils 1.2.3 启动服务-测试 二、yml配置文件 2.1 配置格式 2.1.1 新增 application.yml 2.1.2 启动服务 2.2 XML与prope…

LeetCode438.找到字符串中所有字母异位词

因为之前写过一道找字母异位词分组的题&#xff0c;所以这道题做起来还是比较得心应手。我像做之前那道字母异位词分组一样&#xff0c;先把模板p排序&#xff0c;然后拿滑动窗口去s中从头到尾滑动&#xff0c;窗口中的这段字串也给他排序&#xff0c;然后拿这两个排完序的stri…

Python 基础 -- Tutorial(三)

7、输入和输出 有几种方法可以表示程序的输出;数据可以以人类可读的形式打印出来&#xff0c;或者写入文件以备将来使用。本章将讨论其中的一些可能性。 7.1 更花哨的输出格式 到目前为止&#xff0c;我们已经遇到了两种写值的方法:表达式语句和print()函数。(第三种方法是使…

等保测评--安全区域边界--测评方法

安全子类--边界防护 a) 应保证跨越边界的访问和数据流通过边界设备提供的受控接口进行通信&#xff1b; 一、测评对象 网闸、防火墙、路由器、交换机和无线接入网关设备等提供访问控制功能的设备或相关组件 二、测评实施 1)应核查在网络边界处是否部署访问控制设备&#x…

【SA8295P 源码分析】13 - Android GVM 虚拟机 QUPv3 UART / SPI / I2C功能配置及透传配置

【SA8295P 源码分析】13 - Android GVM 虚拟机 QUPv3 UART / SPI / I2C功能配置及透传配置 一、QUP v3 介绍二、QUP v3 UART 功能配置2.1 TrustZone 域 Uart 资源权限配置:以 QUPV3_0_SE2 为例2.2 QNX Host 域关闭 Uart 资源:以 QUPV3_0_SE2 为例2.3 Android Kernel 域使能 U…

GEE/PIE 遥感大数据处理与典型案例

查看原文>>>【399三天】GEE/PIE遥感大数据处理与典型案例实践 随着航空、航天、近地空间等多个遥感平台的不断发展&#xff0c;近年来遥感技术突飞猛进。由此&#xff0c;遥感数据的空间、时间、光谱分辨率不断提高&#xff0c;数据量也大幅增长&#xff0c;使其越来…

数据结构(6)

2-3查找树 2-结点&#xff1a;含有一个键(及其对应的值)和两条链&#xff0c;左链接指向2-3树中的键都小于该结点&#xff0c;右链接指向的2-3树中的键都大于该结点。 3-结点&#xff1a;含有两个键(及其对应的值)和三条链&#xff0c;左链接指向的2-3树中的键都小于该结点&a…

python中的matplotlib画散点图(数据分析与可视化)

python中的matplotlib画散点图&#xff08;数据分析与可视化&#xff09; import numpy as np import pandas as pd import matplotlib.pyplot as pltpd.set_option("max_columns",None) plt.rcParams[font.sans-serif][SimHei] plt.rcParams[axes.unicode_minus]Fa…

我的动态归纳(便于搜索)

linux dns配置文件是“/etc/resolv.conf”&#xff0c;该配置文件用于配置DNS客户&#xff0c;它包含了主机的域名搜索顺序和DNS/服务器的地址&#xff0c;每一行包括一个关键字和一个或多个空格隔开的参数。 /etc/resolv.conf &#xff08;不配置就不能域名解析&#xff09; 可…