2025-02-06

This commit is contained in:
2025-02-06 22:43:19 +08:00
parent 9480e81242
commit af0d9cdab6
82 changed files with 12564 additions and 0 deletions
@@ -0,0 +1,115 @@
### 1.创建项目
<img src="https://yp.smallkun.cn/markdown/image-20241222165137489.png!compress" alt="image-20241222165137489" style="zoom:50%;" />
创建项目时选择空项目
### 2.配置输出控制台
<img src="https://yp.smallkun.cn/markdown/image-20241222165231278.png!compress" alt="image-20241222165231278" style="zoom:50%;" />
右键项目名称选择`属性`
配置属性->链接器->系统->子系统设置为`控制台 (/SUBSYSTEM:CONSOLE)`
<img src="https://yp.smallkun.cn/markdown/image-20241222165309647.png!compress" alt="image-20241222165309647" style="zoom:50%;" />
### 3.安装连接软件
- 64位编译器选择**Windows (x86, 64-bit), ZIP Archive**(mysql-connector-c-6.1.11-winx64.zip)
- 32位编译器选择**Windows (x86, 32-bit), ZIP Archive**(mysql-connector-c-6.1.11-win32.zip)
https://downloads.mysql.com/archives/c-c/?version=6.1.11&amp;os=src
![image-20240518184737189](https://yp.smallkun.cn/markdown/image-20240518184737189.png!compress!compress)
安装完后会在C:\Program Files (x86)\MySQL\MySQL Connector C 6.1目录下
### 4.配置头文件和lib文件
**配置头文件路径**
C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include
右键项目名称点击属性 找到 配置属性下的VC++目录 将上述路径加入到`包含目录`
<img src="https://yp.smallkun.cn/markdown/image-20241222170322231.png!compress" alt="image-20241222170322231" style="zoom:50%;" />
点击最右侧下拉框后点击编辑
![image-20241222170447324](https://yp.smallkun.cn/markdown/image-20241222170447324.png!compress)
**配置lib文件路径**
C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib
右键项目名称点击属性 找到 配置属性->链接器->常规 将上述路径加入到`附加库目录`
<img src="https://yp.smallkun.cn/markdown/image-20241222170720851.png!compress" alt="image-20241222170720851" style="zoom:50%;" />
**配置附加依赖性**
配置属性——>链接器——>输入——>附加依赖项
<img src="https://yp.smallkun.cn/markdown/image-20241222170820644.png!compress" alt="image-20241222170820644" style="zoom:50%;" />
在其中加入`libmysql.lib`
<img src="https://yp.smallkun.cn/markdown/image-20241222171057161.png!compress" alt="image-20241222171057161" style="zoom:50%;" />
### 5.编写数据库连接代码
```c
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
void init_conn(MYSQL* mysql) {
mysql_init(mysql); // 初始化 MySQL 连接
if (!mysql_real_connect(mysql, "localhost", "root", "root", "user", 3306, NULL, CLIENT_IGNORE_SIGPIPE)) {
printf("连接数据库时发生错误!\n");
printf("%s\n", mysql_error(mysql));
}else{
printf("连接成功!\n");
}
}
int main(void){
MYSQL mysql;
int ret = 0;
init_conn(&mysql);
return 0;
}
```
### 6.配置my.ini
运行代码后会发现
<img src="https://yp.smallkun.cn/markdown/image-20240518190612777.png!compress!compress" alt="image-20240518190612777" style="zoom:50%;" />
要永久地关闭 MySQL 中的 SSL 连接功能,需要进行以下步骤:
编辑 MySQL 配置文件 my.cnf(或 my.ini),一般位于 MySQL 安装目录的 /etc 或 /etc/mysql 子目录下。
并添加以下配置:
Windows下此文件路径在C:\ProgramData\MySQL\MySQL Server 5.7\my.ini
```ini
ssl=0
```
1. 保存文件并退出编辑器。
2. 重启 MySQL 服务。
**测试连接**
![image-20241222171136732](https://yp.smallkun.cn/markdown/image-20241222171136732.png!compress)
+40
View File
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mysql_utils.h"
/*
DBConnection *db_init(const char *host, const char *user, const char *password, const char *database, unsigned int port)
void db_close(DBConnection *db)
int db_common(DBConnection *db, const char *query)
MYSQL_RES *db_query(DBConnection *db, const char *query)
void process_result(MYSQL_RES *result)
*/
void clearUser(DBConnection *db){
db_common(db, "TRUNCATE users");
printf("Çå¿Õ³É¹¦!!! \n");
}
int addUser(DBConnection *db, char *username, char *password){
char sql[100];
sprintf(sql, "INSERT INTO users() VALUES(DEFAULT, \"%s\", \"%s\")", username, password);
printf("%s\n", sql);
return db_common(db, sql);
}
int main(void){
DBConnection *conn;
conn = db_init("localhost", "root", "root", "user", 3306);
//process_result(db_query(conn, "SELECT * FROM users"));
printf("%d\n", addUser(conn, "root", "root"));
return 0;
}
@@ -0,0 +1,108 @@
#ifndef MYSQL_H
#include <mysql.h>
#endif
#ifndef STDIO_H
#include <stdio.h>
#endif
#ifndef STDLIB_H
#include <stdlib.h>
#endif
#ifndef STRING_H
#include <string.h>
#endif
//对MySQL连接指针变量 创建结构体变量并取别名
typedef struct {
MYSQL *conn;
} DBConnection;
DBConnection *db_init(const char *host, const char *user, const char *password, const char *database, unsigned int port) {
//动态内存分配连接的地址
DBConnection *db = (DBConnection *)malloc(sizeof(DBConnection));
if (db == NULL) {
printf("内存分配错误\n");
return NULL;
}
//对连接地址进行动态内存分配
db->conn = mysql_init(NULL);
if (db->conn == NULL) {
printf("初始化失败\n");
free(db);
return NULL;
}
//对连接地址进行配置
if (mysql_real_connect(db->conn, host, user, password, database, port, NULL, 0) == NULL) {
printf("配置连接失败: %s\n", mysql_error(db->conn));
mysql_close(db->conn);
free(db);
return NULL;
}
mysql_set_character_set(db->conn, "GBK");//设置输入输出结果字符串为GBK
return db;
}
//关闭连接函数
void db_close(DBConnection *db) {
if (db != NULL) {
if (db->conn != NULL) {
mysql_close(db->conn);
}
free(db);
}
}
//更新、删除、新增 通用函数
int db_common(DBConnection *db, const char *query) {
if (mysql_query(db->conn, query)) {
printf("common query failed: %s\n", mysql_error(db->conn));
return 0;
}
if (strncmp(query, "INSERT", 6) == 0) {
return (int)mysql_insert_id(db->conn);
}
return (int)mysql_affected_rows(db->conn);
}
//查询通用函数
MYSQL_RES *db_query(DBConnection *db, const char *query) {
MYSQL_RES *result;
if (mysql_query(db->conn, query)) {
printf("查询失败: %s\n", mysql_error(db->conn));
return NULL;
}
result = mysql_store_result(db->conn);
if (result == NULL) {
printf("获取查询结果失败: %s\n", mysql_error(db->conn));
}
return result;
}
//处理查询结果函数
void process_result(MYSQL_RES *result) {
MYSQL_ROW row;
unsigned int num_fields;
unsigned int i;
if (result == NULL) {
fprintf(stderr, "Result set is NULL.\n");
return;
}
//获取字段数
num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result))) {
for (i = 0; i < num_fields; i++) {
printf("%s\t", row[i] ? row[i] : "NULL");
}
printf("\n");
}
}