cpp mysql ubuntu
1.Install libmysqlcppconn-dev
sudo apt-get install libmysqlcppconn-dev
2.
//MySQLHelper.h #include <iostream> #include <mysql_connection.h> #include <mysql_driver.h> #include <mysql_error.h> #include <mysql_connection.h> #include <cppconn/driver.h> #include <cppconn/connection.h> #include <cppconn/statement.h> #include <cppconn/resultset.h> #include <cppconn/exception.h> #include <cppconn/prepared_statement.h> using namespace std; class MySQLHelper { public: void mySqlConnectDemo() { try { sql::Driver *driver; sql::Connection *conn; sql::Statement *stmt; sql::ResultSet *res; sql::ResultSetMetaData *resMeta; sql::PreparedStatement *pstmt; driver = get_driver_instance(); conn = driver->connect("tcp://127.0.0.1:3306", "username", "password"); conn->setSchema("db"); stmt = conn->createStatement(); pstmt = conn->prepareStatement("select * from Book"); res = stmt->executeQuery("select * from Book"); resMeta=res->getMetaData(); int fieldsCount=resMeta->getColumnCount(); while (res->next()) { cout<<res->getInt(1)<<","<<res->getInt64(2)<<","<<res->getString(3)<<","<<res->getString(4)<<"," <<res->getString(5)<<","<<res->getString(6)<<","<<res->getString(7); cout<<endl<<endl; } } catch (const std::exception &e) { std::cerr << e.what() << '\n'; } } };
2.
//main.cpp #include "Model/MySQLHeler.h" void mySqlHelperDemo() { MySQLHelper mh; mh.mySqlConnectDemo(); } int main(int args, char **argv) { mySqlHelperDemo(); }
Compile
g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -ljsoncpp -luuid -lmysqlcppconn
Run
In mysql select result
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2020-12-03 sql server add column with default value
2019-12-03 C# copy folder and files from source path to target path