windows vscode mingw c++入门(3) 连接mysql
windows vscode mingw c++入门(3) 连接mysql自备mysql服务器,准备数据库账号密码下载mysql c库官方包https://dev.mysql.com/downloads/installer/新建目录 clib 放c++ 库main.cpp 中 #include “mysql.h”发现 报错,先创建 c_cpp_properties.json文件...
·
windows vscode mingw c++入门(3) 连接mysql
自备mysql服务器,准备数据库账号密码
下载mysql c库官方包
https://dev.mysql.com/downloads/installer/
新建目录 clib 放c++ 库
main.cpp 中 #include “mysql.h”
- 发现 报错,
- 先创建 c_cpp_properties.json文件
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:\\mingw32\\include",
"D:\\CodeHome\\cpp\\myfirst\\first",
"D:\\CodeHome\\cpp\\lib\\mysql\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\mingw32\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x86"
}
],
"version": 4
}
- 然后在tasks.json 中修改args 命令
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\mingw32\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I", //包含头目录
"D:\\CodeHome\\cpp\\myfirst\\first", //头目录路径
"D:\\CodeHome\\cpp\\myfirst\\first\\q.cpp", //不想一个个编译成 .o文件,直接包含 cpp 文件
"-I",
"D:\\CodeHome\\cpp\\lib\\mysql\\include", // mysql头文件,有顺序要求,要求放在被导入文件之后,
"-L",
"D:\\CodeHome\\cpp\\lib\\mysql\\lib", // mysql 库文件 dll
"-llibmysql" // 导入那个库
],
"options": {
"cwd": "C:\\mingw32\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
- 把 libmysql.dll dll 复制到 与 main.cpp 同一个目录
- 编写代码
#include <iostream>
//#include "q.h"
#include "mysql.h"
using namespace std;
int main()
{
cout << "Hello, world!" << endl;
// q1();
MYSQL conn;
int res;
mysql_init(&conn);
if (mysql_real_connect(&conn, "127.0.0.1", "root", "123", "test", 0, NULL, CLIENT_FOUND_ROWS)) //"root":数据库管理员 ,:123密码 "test":数据库的名字
{
printf("connect success!\n");
res = mysql_query(&conn, "insert into test values('user','123456')");
if (res)
{
printf("error\n");
}
else
{
printf("OK\n");
}
mysql_close(&conn);
}
else
{
printf("connect errot!\n");
}
return 0;
}
- f5 调试,输出connect success, error 是因为sql 命令错误
更多推荐
所有评论(0)