C/C++数据库编程

文章目录

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

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

MySQL安装_win10(超详细)

C/C++访问MySQL数据库

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

  1. 查看防火墙端口开放的情况:
lua 复制代码
service firewalld status
  1. 关闭防火墙:
vbscript 复制代码
systemctl stop firewalld

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

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

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

cpp 复制代码
#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增删改查操作

cpp 复制代码
// 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
cpp 复制代码
// 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;
}
cpp 复制代码
// 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;
}
相关推荐
CVer儿9 分钟前
条件编译代码记录
开发语言·c++
大熊程序猿14 分钟前
python 读取excel数据存储到mysql
数据库·python·mysql
落落落sss16 分钟前
sharding-jdbc分库分表
android·java·开发语言·数据库·servlet·oracle
jnrjian17 分钟前
Oracle 启动动态采样 自适应执行计划
数据库·oracle
.普通人33 分钟前
c语言--力扣简单题目(回文链表)讲解
c语言·leetcode·链表
星迹日34 分钟前
C语言:联合和枚举
c语言·开发语言·经验分享·笔记
程序猿练习生35 分钟前
C++速通LeetCode简单第18题-杨辉三角(全网唯一递归法)
c++·算法·leetcode
知识分享小能手37 分钟前
mysql学习教程,从入门到精通,SQL DISTINCT 子句 (16)
大数据·开发语言·sql·学习·mysql·数据分析·数据库开发
lamb张38 分钟前
MySQL锁
数据库·mysql
Huazzi.38 分钟前
算法题解:斐波那契数列(C语言)
c语言·开发语言·算法