创建一个文件夹test02
在文件夹中创建test.c文件
用vscode打开test02文件夹
自动生成tasks.json和launch.json文件,需要安装这里通C/C++ Runner
插件来自动生成json
文件和一些文件夹。
接下来配置mysql
本地已经安装了mysql数据库,此安装过程省略。
有的人不配置两个json也能运行,但是我运行一直报错,所以我配置了两个json!!!!
进入VSCode,配置两个json文件
1)c_cpp_properties.json
打开vscode,按下ctrl+shit+p,如下,选择编辑C/C++配置,会自动创建一个名为c_cpp_properties.json 的文件
需要在 c_cpp_properties.json 文件中设置 includePath
C++
C
{
"configurations": [
{
"name": "windows-gcc-x64",
"includePath": [
"${workspaceFolder}/**",
"C:\\Program Files\\MySQL\\MySQL Server 5.7\\include"
],
"compilerPath": "D:/software/mingw32/bin/gcc.exe",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
2)tasks.json
tasks.json 用来定义 g++ 的编译指令, -I 和 -L 则指定了编译时 include 头文件和 lib 链接的位置。通常,如果 -I 的内容设置错误,会导致程序中的类或函数无法找到;如果 -L 的内容设置错误,则容易出现运行时的链接错误。
我们找到 MySQL Server 安装路径下所包含的 lib 和 include 目录,将他们包含到这里
C++
C
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "D:/software/mingw32/bin/gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I","C:\\Program Files\\MySQL\\MySQL Server 5.7\\include",
"-L","C:\\Program Files\\MySQL\\MySQL Server 5.7\\lib",
"-llibmysql"
],
"options": {
"cwd": "D:/software/mingw32/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
引入文件
打开MySQL的安装路径,如果是按这篇博客安装的MySQL,安装路径在:C:\Program Files 下 找到MySQL文件夹打开
复制include文件夹到项目test02文件夹中,并改名为mysql
进入MySQL安装目录下的lib
最终test02文件夹内容:
测试代码的编写
mysql C语言 API函数查询网址: https://www.mysqlzh.com/api/1.html
注意:
这段代码的测试前一定要先在MySQL数据库中创建一个test数据库和user表,表有三个字段。如下
CREATE DATABASE test;
CREATE TABLE user(
id CHAR(10) KEY,
uname CHAR(20) NOT NULL,
score INT
);
对表操作测试代码,代码在vs下运行时需要切换到64位平台上,连接数据库的密码和数据库的名称根据自己的需求来修改。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*引入连接Mysql的头文件和lib包*/
#include "mysql/mysql.h"
#pragma comment(lib,"libmysql")
/*定义一些数据库连接需要的宏*/
#define HOST "localhost" /*MySql服务器地址*/
#define USERNAME "root" /*用户名*/
#define PASSWORD "root" /*数据库连接密码*/
#define DATABASE "test" /*需要连接的数据库*/
// 执行sql语句的函数
void exeSql(char* sql) {
MYSQL my_connection; /*数据库连接*/
int res; /*执行sql语句后的返回标志*/
MYSQL_RES* res_ptr; /*执行结果*/
MYSQL_ROW result_row; /*按行返回查询信息*/
MYSQL_FIELD* field; /*返回表字段*/
int row, column; /* 定义行数,列数*/
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, CLIENT_FOUND_ROWS))
{
//printf("数据库连接成功!");
/*vs默认编码为 gbk 防止乱码*/
mysql_query(&my_connection, "set names gbk");
res = mysql_query(&my_connection, sql);
if (res) {
/*现在就代表执行失败了*/
printf("Error: mysql_query !\n");
/*不要忘了关闭连接*/
mysql_close(&my_connection);
}
else
{
/*现在就代表执行成功了*/
/*mysql_affected_rows会返回执行sql后影响的行数*/
printf("%lld 行受到影响!\n", mysql_affected_rows(&my_connection));
// 把查询结果装入 res_ptr
res_ptr = mysql_store_result(&my_connection);
// 存在则输出
if (res_ptr)
{
// 获取行数,列数
row = (int)mysql_num_rows(res_ptr);
column = (int)mysql_num_fields(res_ptr);
//打印字段
field = mysql_fetch_fields(res_ptr);
for (int i = 0; i < column; i++)
{
printf("%s ", field->name);
field++;
}
printf("\n");
// 执行输出结果,从第二行开始循环(第一行是字段名)
for (int i = 1; i < row + 1; i++)
{
// 一行数据
result_row = mysql_fetch_row(res_ptr);
for (int j = 0; j < column; j++)
{
printf("%s ", result_row[j]);
}
printf("\n");
}
}
/*不要忘了关闭连接*/
mysql_close(&my_connection);
}
}
else
{
printf("数据库连接失败!");
}
}
int main()
{
exeSql("insert into user values ('02','张三',90);");
exeSql("insert into user values ('01','李四',90);");
exeSql("select * from user;");
return 0;
}