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

 

posted @   FredGrit  阅读(97)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2020-12-02 WPF MultiSelect MVVM
2019-12-02 C# Monitor and transfer or copy the changed or created file to a new location
点击右上角即可分享
微信分享提示