cppconn cpp create table in cpp file ,show current date time in mysql cli via select current_timestamp command
//Model/mysqlhelper.h #ifndef __mysqlhelper_h__ #define __mysqlhelper_h__ #include <chrono> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <sstream> #include <thread> #include <unistd.h> #include <uuid/uuid.h> #include <mysql/mysql.h> #include <cppconn/connection.h> #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/metadata.h> #include <cppconn/parameter_metadata.h> #include <cppconn/prepared_statement.h> #include <cppconn/resultset.h> #include <cppconn/resultset_metadata.h> using namespace std; using namespace std::chrono; class mysqlhelper { public: string get_time_now(); void createtable(const string &tablename); }; #endif //model/mysqlhelper.cpp #include "model/mysqlhelper.h" string mysqlhelper::get_time_now() { chrono::time_point now = system_clock::now(); chrono::microseconds ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000; time_t rawTime = system_clock::to_time_t(now); struct tm tmInfo = *localtime(&rawTime); stringstream ss; ss << std::put_time(&tmInfo, "%Y%m%d%H%M%S") << std::setfill('0') << std::setw(3) << ms.count(); string dtStr = ss.str(); ss.str(std::string()); return dtStr; } void mysqlhelper::createtable(const string& tablename) { try { sql::Driver *driver; sql::Connection *conn; sql::ResultSet *res; sql::Statement *stmt; sql::PreparedStatement *pstmt; sql::ResultSetMetaData *resMetadata; driver=get_driver_instance(); conn=driver->connect("tcp://127.0.0.1:3306","fred","Fred0001!"); conn->setSchema("db"); stmt=conn->createStatement(); stringstream ss; ss<<"create table "<<tablename; ss<<"(idx int not null auto_increment,id bigint not null default 0,author varchar(40) not null default '',"; ss<<"comment varchar(40) not null default '',content varchar(40) not null default '',name varchar(40) not null default '',"; ss<<"title varchar(40) not null default '',topic varchar(40) not null default '',primary key (`idx`)) "; ss<<"ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"; string createSQL=ss.str(); cout<<createSQL<<endl; bool isCreated=stmt->execute(createSQL); std::cout<<"isCreated="<<isCreated<<endl; ss=stringstream(); ss.str(std::string()); conn->close(); delete stmt; delete conn; cout<<get_time_now()<<",finished in "<<__FUNCTION__<<","<<__LINE__<<endl; } catch (const sql::SQLException &e) { std::cerr << e.what() << "\n"<< e.getErrorCode() << "\n"<< e.getSQLState() << "\n"<< e.getSQLStateCStr() << "\n\n\n"; } } //main.cpp #include "model/mysqlhelper.h" void createtabledemo(const string&tablename) { mysqlhelper mh; mh.createtable(std::ref(tablename)); } int main(int args, char **argv) { createtabledemo(argv[1]); }
Compile
g++ -g -std=c++2a -I. *.cpp ./model/*.cpp -o h1 -luuid -ljsoncpp -lmysqlcppconn
Run
./h1 b3

Create table sql
create table b3(idx int not null auto_increment,id bigint not null default 0,author varchar(40) not null default '',comment varchar(40) not null default '',content varchar(40) not null default '',name varchar(40) not null default '',title varchar(40) not null default '',topic varchar(40) not null default '',primary key (`idx`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
create table b3(idx int not null auto_increment,id bigint not null default 0,author varchar(40) not null default '',comment varchar(40) not null default '',content varchar(40) not null default '',name varchar(40) not null default '',title varchar(40) not null default '',topic varchar(40) not null default '',primary key (`idx`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
In mysql
show create table b3;

select current_timestamp;

 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号