当前位置:网站首页>MySQL and C language connection (vs2019 version)
MySQL and C language connection (vs2019 version)
2022-07-06 11:25:00 【%xiao Q】
1. Choose the appropriate Relase X64 Run the program

2. hold MySQL Of lib Under folder libmysql.dll and libmysql.lib Copy to the project folder
route :C:\Download\Mysql\mysql-8.0.26-winx64\lib

Copy to the changed path :D:\ Class folder \MYSQL Curriculum \ Job introduction information management system \ Job introduction information management system 
3. add to libmysql.lib To attach dependencies : attribute -> The linker -> Input -> Additional dependency

4. add to include Attach to include directory : attribute -> c/c++ -> routine -> Attach include directory

include route :C:\Download\Mysql\mysql-8.0.26-winx64\include
MySQL Initialization and definition of variables
// Included header file
#include <stdio.h>
#include <WinSock.h>
#include <mysql.h>
MYSQL m; // mysql Connect
MYSQL_RES* res; // Query result set
MYSQL_ROW row; // Two dimensional array , Storing data
// initialization
mysql_init(&m);
// Set the encoding mode
mysql_options(&m, MYSQL_SET_CHARSET_NAME, "gbk");
Database connection
// “ host ”,“ user name ”,“ password ”,“ Database name ”,“ port ”
if (mysql_real_connect(&m, "localhost", "root", "0915", "test", 3306, NULL, 0))
printf(" Database connection successful \n");
else
printf(" Database connection failed %s\n", mysql_error(&m)); // Output error message
Insert data into the database
// Define a sql sentence
const char* sql = "insert into stu values('1001', ' Xiao Ming ', 100)";
// mysql_query(&m, sql) To perform a sql sentence , Successfully returns 0, Failure returns to non 0
if (mysql_query(&m, sql))
printf(" Insert data failed %s\n", mysql_error(&m));
else
printf(" Insert data succeeded \n");
// Insert multiple pieces of data into the database
const char* sql = "insert into stu values('1002',' Xiaohong ', 98), ('1005', 'baby', 89), ('1004', 'asd', 95)";
if (mysql_query(&m, sql))
printf(" Insert data failed :%s", mysql_error);
else printf(" Insert the success ");
Delete data
const char* sql = "delete from stu where id = '1005'";
if (mysql_query(&m, sql))
printf(" Delete failed :%s", mysql_errno);
else
printf(" Delete successful \n");
Update data
const char* sql = "update stu set id = '1003' where name = 'asd'";
if (mysql_query(&m, sql))
printf(" Update failed :%s", mysql_error);
else
printf(" The update is successful \n");
Query data
// Query data
const char* sql = "select * from stu";
if (mysql_query(&m, sql))
printf(" Not found :%s", mysql_error);
else
printf(" Find success \n");
// Get the query result set
res = mysql_store_result(&m);
if (res)
printf(" Get data \n");
else
printf(" No data obtained :%s", mysql_error);
// Print data
while (row = mysql_fetch_row(res))
{
printf("%s\t%s\t%s\t\n", row[0], row[1], row[2]);
}
// Release result set
mysql_free_result(res); // Release result set
mysql_close(&m); // Close the database
A source code is attached below
#include <stdio.h>
#include <WinSock.h>
#include <mysql.h>
void test()
{
MYSQL m; // mysql Connect
MYSQL_RES* res; // Query result set
MYSQL_ROW row; // Two dimensional array , Storing data
// initialization
mysql_init(&m);
// Set the encoding mode
mysql_options(&m, MYSQL_SET_CHARSET_NAME, "gbk");
// Connect to database
// “ host ”,“ user name ”,“ password ”,“ Database name ”,“ port ”
if (mysql_real_connect(&m, "localhost", "root", "0915", "test", 3306, NULL, 0))
printf(" Database connection successful \n");
else
printf(" Database connection failed %s\n", mysql_error(&m)); // Output error message
// Insert data into the database
const char* sql = "insert into stu values('1001', ' Xiao Ming ', 100)";
if (mysql_query(&m, sql))
printf(" Insert data failed %s\n", mysql_error(&m));
else
printf(" Insert data succeeded \n");
// Insert multiple pieces of data into the database
const char* sql = "insert into stu values('1002',' Xiaohong ', 98), ('1005', 'baby', 89), ('1004', 'asd', 95)";
if (mysql_query(&m, sql))
printf(" Insert data failed :%s", mysql_error);
else printf(" Insert the success ");
// Delete data
const char* sql = "delete from stu where id = '1005'";
if (mysql_query(&m, sql))
printf(" Delete failed :%s", mysql_errno);
else
printf(" Delete successful \n");
// Update data
const char* sql = "update stu set id = '1003' where name = 'asd'";
if (mysql_query(&m, sql))
printf(" Update failed :%s", mysql_error);
else
printf(" The update is successful \n");
// Query data
//string s = "select * from stu";
//const *sql = s;
char sql[] = "select * from stu";
if (mysql_query(&m, sql))
printf(" Not found :%s", mysql_error);
else
printf(" Find success \n");
// Get the query result set
res = mysql_store_result(&m);
if (res)
printf(" Get data \n");
else
printf(" No data obtained :%s", mysql_error);
// Print data
while (row = mysql_fetch_row(res))
{
printf("%s\t%s\t%s\t\n", row[0], row[1], row[2]);
}
// Release result set
mysql_free_result(res); // Release result set
mysql_close(&m); // Close the database
}
int main()
{
//cout << "main" << endl;
test();
getchar();
return 0;
}
边栏推荐
- Project practice - background employee information management (add, delete, modify, check, login and exit)
- What does BSP mean
- Did you forget to register or load this tag 报错解决方法
- UDS learning notes on fault codes (0x19 and 0x14 services)
- JDBC原理
- vs2019 第一个MFC应用程序
- 01项目需求分析 (点餐系统)
- Kept VRRP script, preemptive delay, VIP unicast details
- 数数字游戏
- JDBC principle
猜你喜欢
![[download app for free]ineukernel OCR image data recognition and acquisition principle and product application](/img/1b/ed39a8b9181660809a081798eb8a24.jpg)
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application

Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'

How to configure flymcu (STM32 serial port download software) is shown in super detail

引入了junit为什么还是用不了@Test注解

打开浏览器的同时会在主页外同时打开芒果TV,抖音等网站

QT creator custom build process

人脸识别 face_recognition
![[Thesis Writing] how to write function description of jsp online examination system](/img/f8/13144e0febf4a576bbcc3290192079.jpg)
[Thesis Writing] how to write function description of jsp online examination system

PHP - whether the setting error displays -php xxx When PHP executes, there is no code exception prompt

In the era of DFI dividends, can TGP become a new benchmark for future DFI?
随机推荐
Ubuntu 20.04 安装 MySQL
学习问题1:127.0.0.1拒绝了我们的访问
图像识别问题 — pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
neo4j安装教程
数数字游戏
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
Install mysql5.5 and mysql8.0 under windows at the same time
[蓝桥杯2017初赛]方格分割
PHP - whether the setting error displays -php xxx When PHP executes, there is no code exception prompt
Redis的基础使用
Test objects involved in safety test
Picture coloring project - deoldify
Ansible practical series I_ introduction
JDBC原理
Codeforces Round #771 (Div. 2)
Cookie setting three-day secret free login (run tutorial)
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
[recommended by bloggers] asp Net WebService background data API JSON (with source code)
MySQL master-slave replication, read-write separation
Ansible practical Series II_ Getting started with Playbook