C++动态链接MySQL库
2024-10-20 03:22:35
C++链接MySQL库
库安装目录
CMakeList
cmake_minimum_required(VERSION 3.22)
project(MySQLConnectionPool)
include_directories(/usr/include/mysql) #安装库路径
set(CMAKE_CXX_STANDARD 17)
add_executable(MySQLConnectionPool main.cpp)
target_link_libraries(MySQLConnectionPool mysqlclient)
测试代码
//1.test.c
#include<cstdio>
#include "mysql/mysql.h"
int main() {
MYSQL * mysql = NULL;
if ((mysql = mysql_init(NULL)) == NULL) {
fprintf(stderr, "error in mysql_init\n");
exit(1);
}
mysql = mysql_real_connect(mysql, "localhost", "root", "lhh123456", "Test", 3306, 0, 0);
if (!mysql) {
fprintf(stderr, "connection failed!\n");
exit(1);
}
printf("Success!\n");
return 0;
}
不使用连接池
使用连接池
一定注意使用show variables like "max_connections";
查看MySQL最大连接数,连接池最大连接不能超过这个数,使用set GLOBAL max_connections=1000;
可以修改最大连接数
void test(int i) {
char sql[512]{0};
Connection conn;
std::this_thread::sleep_for(std::chrono::milliseconds(2));
sprintf(sql, "insert into Info(ID,UserName,IDCard,Passwd)values(%d,'%s','%s','%s');",
i, "zhangsan", std::to_string(i).c_str(), "123456");
conn.connect("127.0.0.1", 3306, "root", "lhh123456", "Test");
conn.update(sql);
}
void test1(int i) {
char sql[512]{0};
ConnectionPool *pool = ConnectionPool::getConnectionPool();
std::shared_ptr<Connection> sp = pool->getConnection();
sprintf(sql, "insert into Info(ID,UserName,IDCard,Passwd)values(%d,'%s','%s','%s');",
i, "zhangsan", std::to_string(i).c_str(), "123456");
sp->update(sql);
}
最新文章
- linux部署war包方案
- SQL语句修改表字段名/修改字段长度/增加字段/删除字段
- drupal 2016-11-3
- SQL server基本操作(一)
- [Browsable(false)]
- bzoj 3637: Query on a tree VI 树链剖分 &;&; AC600
- 2013 ACM/ICPC 长沙现场赛 A题 - Alice&#39;s Print Service (ZOJ 3726)
- OC5_构造方法与self指针
- web前端 - 模态对话框
- MobileNets总结
- 洛谷 [P1341]无序字母对
- 数据库学习番外篇 神奇的Redis
- (六)jdk8学习心得之Stream流
- httpClient4.5 closeableHttpClient用法
- 开源播放器 ijkplayer (五) :Linux/Ubuntu 下编译ijkplayer
- CF528D Fuzzy Search
- Javascript \x 反斜杠x 16进制 编解码
- python爬虫之解析库Beautiful Soup
- 排序算法<;No.5>;【堆排序】
- python Requests库网络爬取IP地址归属地的自动查询