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);
}

最新文章

  1. linux部署war包方案
  2. SQL语句修改表字段名/修改字段长度/增加字段/删除字段
  3. drupal 2016-11-3
  4. SQL server基本操作(一)
  5. [Browsable(false)]
  6. bzoj 3637: Query on a tree VI 树链剖分 &amp;&amp; AC600
  7. 2013 ACM/ICPC 长沙现场赛 A题 - Alice&#39;s Print Service (ZOJ 3726)
  8. OC5_构造方法与self指针
  9. web前端 - 模态对话框
  10. MobileNets总结
  11. 洛谷 [P1341]无序字母对
  12. 数据库学习番外篇 神奇的Redis
  13. (六)jdk8学习心得之Stream流
  14. httpClient4.5 closeableHttpClient用法
  15. 开源播放器 ijkplayer (五) :Linux/Ubuntu 下编译ijkplayer
  16. CF528D Fuzzy Search
  17. Javascript \x 反斜杠x 16进制 编解码
  18. python爬虫之解析库Beautiful Soup
  19. 排序算法&lt;No.5&gt;【堆排序】
  20. python Requests库网络爬取IP地址归属地的自动查询

热门文章

  1. Shell 脚本实践指南
  2. 使用filebeat过滤掉部分字段
  3. 使用kubeoperator自带的nginx-ingress-controller设置服务的ingress规则进行访问
  4. MySQL数据表更新模板
  5. 初试 Ceph 存储之块设备、文件系统、对象存储
  6. 《Deep Feature Extraction and Classification of Hyperspectral Images Based on Convolutional Neural Networks》论文笔记
  7. 使用css制作轮播图
  8. 常见的 Kerberos 错误消息
  9. String类型变量的使用
  10. MySQL开发