①c语言连接云服务器MySQL 环境:VS2022

c语言连接MySQL 环境:VS2022

一、Windows下安装VS2022

参考链接VS2019安装教程(超详细)新手必码

二、安装MySQL

Linux云服务器上直接通过宝塔面板安装MySQL

宝塔面板https://www.bt.cn/new/download.html

同时Windows下安装MySQL,主要是需要头文件和依赖文件

自行选择版本https://downloads.mysql.com/archives/community/

解压成功后记录下位置

位置为C:\Users\锐锐\Downloads\mysql-5.7.34-winx64

三、vs中新建openssl项目设置

打开VS2022

文件→新建→项目→空项目

LejQKS.png

新建MySQL_test测试项目

LejJ5n.png

添加源文件

在右侧的“搜索解决方案资源管理器”栏目中,项目名下“源文件”处,右键→添加→现有项

LexHjf.png

添加之前在openeuler下运行的代码文件

链接MySQL设置

图用的之前的openssl的了,原理都是一样的

在右侧的“搜索解决方案资源管理器”栏目中,项目名下,右键→属性→VC++目录处

LezZ59.png

“包含目录”处,添加MySQL解压目录下的include文件夹

LezjsK.png

“库目录”处,添加MySQL解压目录下的lib文件夹

LmSiRI.png

添加完如图

LmSVL8.png

接着将MySQL解压文件里lib文件夹里的libmysql.dll libmysql.lib 复制到项目目录里

添加libmysql.lib到附加依赖项:属性 -> 链接器 -> 输入 -> 附加依赖项

注意用;分开

四、MySQL中新建用户数据库,创建测试用表与插入数据

在宝塔面板中安装好数据库后进入phpmyadmin,创建新用户,并允许外网访问

登录新用户,进入新建的数据库建表并插入数据

CREATE TABLE Student
(Sno CHAR(9) PRIMARY KEY, /* 列级完整性约束条件/
Sname CHAR(20) UNIQUE, /
Sname取唯一值*/
Ssex CHAR(3),
Sage SMALLINT,
Sdept CHAR(20)
);
CREATE TABLE Course
( Cno CHAR(4) PRIMARY KEY,
Cname CHAR(40),
Cpno CHAR(4) ,
Ccredit SMALLINT,
FOREIGN KEY (Cpno) REFERENCES Course(Cno)
);

CREATE TABLE SC
(Sno CHAR(9),
Cno CHAR(4),
Grade SMALLINT,
PRIMARY KEY (Sno,Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno),
FOREIGN KEY (Cno) REFERENCES Course(Cno)
);

INSERT INTO Student VALUES ('200215126','张成民','男',18,'CS');
INSERT INTO Student VALUES ('200215127','王芳','女',17,'CS');
INSERT INTO Student VALUES ('200215128','李小清','女',18,'IS');
INSERT INTO Student VALUES ('200215129','赵先军','男',19,'CS');
INSERT INTO Student VALUES ('200215130','王思玉','女',18,'IS');
INSERT INTO Student VALUES ('200215131','张为民','男',18,'CS');
INSERT INTO Student VALUES ('200215132','刘贤','男',20,'CS');
INSERT INTO Student VALUES ('200215133','武城山','男',18,'CT');
INSERT INTO Student VALUES ('200215134','刘建国','男',18,'CT');
INSERT INTO Student VALUES ('200215135','田军','男',18,'CS');

INSERT INTO Course VALUES ('0204','离散数学',null,3);
INSERT INTO Course VALUES ('0203','计算机网络',null,3);
INSERT INTO Course VALUES ('0206','操作系统',null,4);
INSERT INTO Course VALUES ('0208','组成原理',null,2);
INSERT INTO Course VALUES ('0210','图形学',null,3);
INSERT INTO Course VALUES ('0207','面向对象程序设计',null,2);
INSERT INTO Course VALUES ('0202','数据结构','0204',4);
INSERT INTO Course VALUES ('0201','数据库','0202',3);
INSERT INTO Course VALUES ('0205','编译原理','0206',2);
INSERT INTO Course VALUES ('0209','软件工程','0201',2);

INSERT INTO SC VALUES ('200215126','0204',80);
INSERT INTO SC VALUES ('200215126','0209',78);
INSERT INTO SC VALUES ('200215126','0210',90);
INSERT INTO SC VALUES ('200215127','0204',60);
INSERT INTO SC VALUES ('200215127','0210',50);
INSERT INTO SC VALUES ('200215127','0207',55);
INSERT INTO SC VALUES ('200215129','0202',98);
INSERT INTO SC VALUES ('200215130','0202',70);

也可以用navicat连接,直接允许sql文件

五、运行代码

include <stdio.h>

include <mysql.h>

int main()
{

MYSQL* my = mysql_init(NULL);

if (!mysql_real_connect(my, "122.112.198.128", "rui1220", "z159753Z", "rui1220", 3306, NULL, 0)) {
    printf("connect error !\n");
    mysql_close(my);
}
const char* sql1 = "select * from Student;";
mysql_query(my, "SET NAMES 'GB2312' ");
//mysql_query(my, "INSERT  INTO  Student    VALUES ('200215136','张民','男',18,'CS'); ");
int res = mysql_query(my, sql1);
//printf("%d", res);
MYSQL_RES* a = mysql_store_result(my);
int rows = mysql_num_rows(a);
int cols = mysql_num_fields(a);
//printf("rows: %d, cols: %d", rows, cols);
MYSQL_FIELD* field = mysql_fetch_fields(a);
for (int i = 0; i < cols; i++)
{
    printf("%-10s\t", field[i].name);
}
puts("");
MYSQL_ROW line;
for (int i = 0; i < rows; i++)
{
    line = mysql_fetch_row(a);
    for (int j = 0; j < cols; j++)
    {
        printf("%-10s\t", line[j]);
    }
    puts("");
}
mysql_close(my);
return 0;

}

posted @ 2022-05-21 11:00  ewdbdvdv  阅读(643)  评论(0编辑  收藏  举报