C++ execute create table sql
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <mysql_connection.h> #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/resultset.h> #include <cppconn/statement.h> #include <mysql_driver.h> #include <cppconn/prepared_statement.h> #include <uuid/uuid.h> #include <sstream> void execSQL8() { string createSQL = "create table BookTable2" "(ID int primary key auto_increment," "BookId varchar(40) not null," "BookAuthor varchar(40)," "BookTitle varchar(40)," "BookISBN varchar (40)," "BookComment longblob);"; executeSQL(createSQL); } void executeSQL(string sqlStr) { try { sql::mysql::MySQL_Driver *driver; sql::Connection *conn; sql::Statement *stmt; driver = sql::mysql::get_mysql_driver_instance(); conn = driver->connect("tcp://127.0.0.1:3306", "user", "password"); conn->setSchema("MyDB"); stmt = conn->createStatement(); stmt->execute(sqlStr); delete stmt; delete conn; } catch (const sql::SQLException &e) { cout << "#ERR:SQLException in " << __FILE__ << ",function in " << __FUNCTION__ << " on line " << __LINE__ << endl; cout << "#ERR:" << e.what() << endl; cout << "(MySQL error code: " << e.getErrorCode() << endl; cout << "SQLState:" << e.getSQLState() << endl; } } int main() { execSQL8(); return 0; }
int main() { exec9(); return 0; } void exec9() { string sql="Create Procedure Sp_InsertIntoBookTable7( bId varchar(40), bAuthor varchar(40), bTitle varchar(40), bISBN varchar(40), bComment longblob) BEGIN insert into BookTable2(BookId,BookAuthor,BookTitle,BookISBN,BookComment) VALUES (bId,bAuthor,bTitle,bISBN,bComment); END"; executeSQL(sql); }
void sp7() { sql::Connection *conn = getMySQLConn();
sql::PreparedStatement *prepStmt; string tableName = "BookTable1"; conn->setSchema("MyDB"); prepStmt = conn->prepareStatement("CALL Sp_InsertIntoBookTable7(?,?,?,?,?)"); struct bookStruct *p; p = bsArrayP5(); std::stringstream ss; for (int i = 0; i < 100; i++) { prepStmt->setString(1, (p + i)->BookId); prepStmt->setString(2, (p + i)->BookAuthor); prepStmt->setString(3, (p + i)->BookTitle); prepStmt->setString(4, (p + i)->BookISBN); ss = stringstream((p + i)->BookComment); prepStmt->setBlob(5, &ss); prepStmt->execute(); ss = stringstream(); } }
g++ -g -std=c++11 -I .../include -L .../lib64 h1.cpp -lmysqlcppconn -luuid -o h1 ./h1