#include <chrono> #include <cstring> #include <ctime> #include <fstream> #include <iomanip> #include <iomanip> #include <iostream> #include <memory> #include <mutex> #include <queue> #include <random> #include <sstream> #include <thread> #include <vector> #include <cppconn/driver.h> #include <cppconn/connection.h> #include <cppconn/metadata.h> #include <cppconn/parameter_metadata.h> #include <cppconn/prepared_statement.h> #include <cppconn/resultset.h> #include <cppconn/resultset_metadata.h> #include <cppconn/statement.h> std::string get_time_now(bool is_exact = true) { std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now(); time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now); struct tm tm_info = *localtime(&raw_time); std::stringstream ss; ss << std::put_time(&tm_info, "%Y%m%d%H%M%S"); if (is_exact) { auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()); auto mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()); auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()); ss << "_" << (mills.count() - seconds.count() * 1000) << std::setfill('0') << std::setw(3) << (micros.count() - mills.count() * 1000) << std::setfill('0') << std::setw(3) << (nanos.count() - micros.count() * 1000); } return ss.str(); } sql::Connection *conn; sql::Connection *mysql_get_conn(const std::string &hostname_value = "localhost", const std::string &username_value = "rumination", const std::string &password_value = "Rumination0001!", const std::string &dbname_value = "db", const bool &isoptreconnect_value = true) { try { sql::Driver *driver = get_driver_instance(); conn = driver->connect(hostname_value, username_value, password_value); conn->setSchema(dbname_value); return conn; } catch (const std::exception &e) { std::stringstream ss; ss << "notify-send 'Connect mysql failed!' '"; ss << e.what() << "!'"; system(ss.str().c_str()); } return conn; } sql::ResultSet *mysql_select_result(const std::string &select_str) { sql::ResultSet *res; try { conn = mysql_get_conn(); sql::Statement *stmt = conn->createStatement(); res = stmt->executeQuery(select_str); } catch (const std::exception &e) { std::stringstream ss; ss << "notify-send 'get select result failed' '"; ss << select_str << "!'"; system(ss.str().c_str()); } return res; } bool update_sql_result(const std::string &update_str) { try { conn = mysql_get_conn(); sql::Statement *stmt = conn->createStatement(); return stmt->execute(update_str); } catch (const std::exception &e) { std::stringstream ss; ss << "notify-send 'update failed' '"; ss << update_str << "!'"; system(ss.str().c_str()); return true; } return true; } void mysql_get_conn_demo() { sql::Connection *conn = mysql_get_conn(); std::cout << std::boolalpha << "conn->isValid():" << conn->isValid() << std::endl; } void mysql_select_demo() { std::string select_str = "select * from t3 limit 10;"; sql::ResultSet *res = mysql_select_result(select_str); sql::ResultSetMetaData *res_metadata = res->getMetaData(); int rows_count = res->rowsCount(); int cols_count = res_metadata->getColumnCount(); std::cout << "Rows count " << rows_count << ",columns count:" << cols_count << std::endl; while (res->next()) { for (int j = 1; j < cols_count; j++) { std::cout << res->getString(j) << "\t"; } std::cout << res->getString(cols_count) << std::endl; } res->close(); std::cout << get_time_now() << ",thread id:" << std::this_thread::get_id() << std::endl; } void mysql_update_demo() { std::stringstream ss; ss << "update t3 set author='" << "aaa" << "' where id=1;"; bool is_succeed = update_sql_result(ss.str()); if (!is_succeed) { std::cout << ss.str() << " execute successfully!" << std::endl; } else { std::cout << ss.str() << " execute failed!" << std::endl; } std::cout << get_time_now() << ",thread id:" << std::this_thread::get_id(); } int main(int args, char **argv) { // g++-13 -g -std=c++23 -I. main.cpp -lmysqlcppconn -luuid -pthread -o h1; mysql_select_demo(); if (conn->isValid()) { conn->close(); } std::cout << get_time_now() << ",finished id: " << std::this_thread::get_id() << " of " << __LINE__ << " in " << __FUNCTION__ << std::endl; }
Compile
g++-13 -g -std=c++23 -I. main.cpp -lmysqlcppconn -luuid -pthread -o h1;
Run
./h1;
标签:std,now,chrono,ss,libmysqlcppconn,mysql,update,time,include From: https://www.cnblogs.com/Fred1987/p/17895040.html