zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【SQL】开窗函数简介
    【博客园美化】参考链接汇总(持续更新中……)
    【SQL】牛客网SQL试题练习(更新到11题)
    【SQL】SQL和MySQL语句的执行顺序
    【数据分析项目】淘宝用户行为分析【SQL+Tableau】
    【数据分析项目】婴儿商品消费情况分析【Excel】
    【Sublime Text 3】前端常用快捷键
    【Excel】常用快捷键
    MongoDB 规范
    mongodb 常用命令
  • 原文地址:https://www.cnblogs.com/Fred1987/p/15633757.html
Copyright © 2011-2022 走看看