Use c language to connect to mysql database and insert data in batches

​ To use C++ to connect to the database, you need to install the database locally, or get the IP address of the host where the database is located. Let me first explain that I am using a mysql8.0 64-bit database, and the vs is vs2019.

1. Configure the environment

   First open vs2019, create an empty project, then right-click the position shown in the figure below, then find the bottom property, click to enter

 Go to the page below, click the VC++ directory, then find Program Files in the computer file C disk, find MySQL, find MySQL Server 8.0, enter the include folder, and put the file address C:\Program Files\MySQL\MySQL Server 8.0\include Copy it to position 2 in the figure below, then return to the previous level to enter the folder lib, and copy the file address C:\Program Files\MySQL\MySQL Server 8.0\lib to the position in figure 3 below.

Open the linker, click enter, click Additional Dependencies, in the folder go to the lib folder

find libmysql.lib

 

 Copy the name of libmysql.lib to the location shown below

Finally click Apply.

     Then enter the lib folder, copy libmysql.lib in the figure below to C:\Windows\System32 in the C drive

 

2. c language link database

    code display

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<mysql.h>//数据库包含的头文件
#include<time.h>

int main()
{
	//固定不变的
	MYSQL mysql;    //一个数据库结构体  
	MYSQL_RES* res; //一个结果集结构体
	MYSQL_ROW row; //char** 二维数据,存放一条条记录
	//初始化数据库
	mysql_init(&mysql);
	//设置编码方式
	mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk");

	//连接数据库                       
	if (mysql_real_connect(&mysql, "ip地址", "数据库的用户名 ", "数据库密码", "使用的数据库名", 3306, NULL, 0) == NULL)
	{
		printf("错误原因:%s\n", mysql_error(&mysql));
		printf("连接失败\n");
		exit(-1);
	}
	char* str1 = "insert into rand values(";//这里使用的表是事先创建好的
	char sql_insert[200];
	sprintf(sql_insert, "%s%d,'%s','%s',%d%s", str1, 3, "xiaoli", "1234a", 3, ")");//批量插入数据
	mysql_query(&mysql, sql_insert);
	//查询数据
	mysql_query(&mysql, "select * from rand");
	//获取结果集
	res = mysql_store_result(&mysql);
	//给ROW赋值,判断ROW是否为空,不为空就打印数据
	while (row = mysql_fetch_row(res))
	{
		printf("%s \n", row[0]);
	}
	//释放结果集
	mysql_free_result(res);
	//关闭数据库
	mysql_close(&mysql);
	//停留等待
	system("pause");
	return 0;

}

    3. Class Introduction

  1. MYSQL handle class

1. MYSQL mysql;

This is a database handle class, which is required for all database operations

   2.MYSQL_RES

2.MYSQL_RES* res;

This is a query result set, which is used to store the values ​​after querying the database

  3.MYSQL_ROW

2.MYSQL_ROW row

This is not a class in essence, it is essentially equivalent to a pointer to a string array, that is, a secondary pointer char**. The function of this statement is to read out the internal use of the MYSQL_RES query result set line by line. A column in the table is an element equivalent to a secondary pointer, and there are several pointers for as many columns as there are in the table.

    This is the first time I write a blog, there are mistakes, I hope everyone can correct them.

Guess you like

Origin blog.csdn.net/weixin_62859191/article/details/125663580