当前位置:网站首页>Windows系统下C语言连接MySQL
Windows系统下C语言连接MySQL
2022-06-21 14:45:00 【用户6978604】
前言
高级语言连接数据库技术可谓十分成熟,但是使用C语言连接数据库还是挺少的,之前做数据库课程设计的时候刚好用到C语言连接数据库,因此就把这次过程记录下来。
准备
- MySQL 5.6.48-64位
- MinGW64
- ==假如你安装的MySQL是32位的,那么MinGW就需要安装32位版本的==
- Visual Studio Code(其他编辑器也可以,有条件的可以用记事本)
开始搭建环境
配置MinGW64环境:
假如是从这里下载的,将压缩文件解压到自定义位置,记住该位置
依次点击“此电脑”,“属性”,“高级系统设置”,“环境变量”,在系统变量中找到Path,增加一条记录,记录值是MinGW64安装路径下的bin文件夹
在终端输入gcc时假如输出下面的语句就说明配置好了
gcc: fatal error: no input files
compilation terminated.项目中引入库文件
- 所在项目处新建文件夹
lib,在MySQL的安装目录下的lib文件夹下找到libmysql.dll文件(我的是在C:\Program Files\MySQL\MySQL Server 5.6\lib文件夹下),将该文件复制到刚刚新建的lib文件夹中 - 在程序中加入头文件:
#include <C:\Program Files\MySQL\MySQL Server 5.6\include\mysql.h>(该路径自己根据自己的情况进行相应更改)#include <winsock.h>
测试
例如我有一个数据库test,我要访问的是stu表:
在项目位置新建main.c文件:
/*
* @Author: YaleXin
* @Date: 2020-07-03 14:17:45
* @LastEditTime: 2020-07-03 14:53:58
* @LastEditors: YaleXin
* @Description:
* @FilePath: \my_c_workspace\DatabaseConnect\main.c
* @祈祷不出现BUG
*/
#include <winsock.h>
#include <C:\Program Files\MySQL\MySQL Server 5.6\include\mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define HOST "localhost"
#define PORT 3306
#define DBNAME "test"
#define TABLENAME "stu"
#define USERNAME "root"
#define PASW "123456"
int main() {
MYSQL mysql;
MYSQL_ROW row;
MYSQL_RES *res = (MYSQL_RES *)malloc(sizeof(MYSQL_RES));
mysql_init(&mysql);
if (mysql_real_connect(&mysql, HOST, USERNAME, PASW, DBNAME, PORT, NULL,
CLIENT_FOUND_ROWS)) {
printf("ok!\n");
char sql[50] = "";
// 告知此次是gbk
mysql_set_character_set(&mysql, "gbk");
sprintf(sql, "SELECT * FROM %s", TABLENAME);
if (mysql_real_query(&mysql, sql, (unsigned long)strlen(sql))) {
printf("查询失败,错误信息:%s", mysql_error(&mysql));
} else {
res = mysql_store_result(&mysql);
printf("|%-10s|%-10s|\n\n", "学号", "姓名");
while ((row = mysql_fetch_row(res)) != NULL) {
printf("|%-10s|%-10s|\n\n", row[0], row[1]);
}
mysql_free_result(res);
}
// 关闭连接,避免浪费资源
mysql_close(&mysql);
} else {
printf("登录失败!错误原因:%s\n是否退出:0:是,其他:否。\n",
mysql_error(&mysql));
}
return 0;
}利用命令行进行编译,进入该项目位置,打开命令终端,输入:
gcc -Iinclude -Llib main.c -llibmysql -o main如果足够幸运的话就会产生main.exe文件,运行该文件,输出:
ok!
|学号 |姓名 |
|1 |张三 |
|2 |李四 |
|3 |王五 |可能遇到的问题
1.
error: unknown type name 'SOCKET'
#define my_socket SOCKET 加入头文件的顺序中,#include <winsock.h>需要在#include <C:\Program Files\MySQL\MySQL Server 5.6\include\mysql.h>上面
2.
File format not recognized collect2.exe: error: ld returned 1 exit status 不知道是不是因为安装的MySQL是64位,而编译器MinGw是32位造成的,我尝试用32位进行编译的时候会出现上面的错误,换了64位的就不会出现问题了。
边栏推荐
- JS written test question: this
- Reptile Foundation_ urllib
- Usage of SED (replacement, deletion of text content, etc.)
- Interview - difference between pointer and reference
- Redis learning (1) -- overview and common commands
- Win10 installation and configuration mongodb
- MySQL failover and master-slave switchover based on MHA
- Operator Tour (I)
- HSV color model and color component range in opencv
- Color segmentation based on RGB difference method
猜你喜欢

Viewing tcp/ip network communication from the sending of an email

Redis introduction and Practice (with source code)

Promotion guide for large enterprises: material preparation, PPT writing and on-site defense
![Cool background shadow effect [second with layered feeling] [picture hover style]](/img/ca/d68f2cf9f9af7b9346032b2a6e120b.jpg)
Cool background shadow effect [second with layered feeling] [picture hover style]

Fundamentals of C language 13: file input / output

So many statistical charts? This visualizer is great~~
![[how to install MySQL 8.0 to a non system disk] [how to create a new connection with Navicat and save it to a non system disk] and [uninstall MySQL 8.0]](/img/e4/895cc829e34692a069923e226deb11.jpg)
[how to install MySQL 8.0 to a non system disk] [how to create a new connection with Navicat and save it to a non system disk] and [uninstall MySQL 8.0]

Qt-3-basic assembly 2

!!** The test is valid * *:vscode "the terminal will be reused by the task, press any key to close" /vscode the terminal runs NPM to pop up the select program dialog box / try a new cross platform Pow

Summary of the most basic methods of numpy
随机推荐
JS written test question: array
Color segmentation based on RGB difference method
Kubeneters' CNI network plug-in installation Kube ovn
Read distributed consistency protocols 2pc and 3pc
Summary of common libraries in machine learning
Win10 installation and configuration mongodb
2022 Fujian latest fire protection facility operator simulation test question bank and answers
T32 custom menu bar
Write a compile time annotation
理财产品的赎回时间是怎么规定的?
What are the log files in MySQL?
Office operation sorting notes
Three versions realize black-and-white theme switching
Simplified crud using code generation
Redis5.0 installation and production startup steps
Reptile essential_ regular expression
ARP interaction process
ES6 test questions
[how to install MySQL 8.0 to a non system disk] [how to create a new connection with Navicat and save it to a non system disk] and [uninstall MySQL 8.0]
启牛学堂app下载证券开户,是安全的吗?有风险嘛?