• C++ mysql_client insert into table


    1.Source code in h1.cpp

    #include <iostream>
    #include <mysql/mysql.h>
    
    using namespace std;
    
    void mysqlClientInsert2();
    
    int main(int args, char **argv)
    {
        mysqlClientInsert2();
    }
    
    void mysqlClientInsert2()
    {
        try
        { 
            MYSQL *conn, mysql;
            int state;
    
            mysql_init(&mysql);
            conn = mysql_real_connect(&mysql, "localhost", "root", "Root0001!", "myDB", 0, 0, 0);
    
            if (conn == NULL)
            {
                cout << mysql_error(&mysql) << endl;
                return;
            }
    
            string str = "Insert into mt(BookId,BookName,BookTitle) values(1,'C++','C++ Programming Books!')";
            state=mysql_query(conn, str.c_str());
            cout<<"State ="<<state<<endl;
        }
        catch (const std::exception &e)
        {
            std::cerr << e.what() << '\n';
        }
    }

    2.Compile via c++ as below command

    g++ -g -std=c++2a -I. -Wall -I/usr/include/mysql/ *.cpp -o h1 -L/usr/lib -lmysqlclient;

    3.Execute

    ./h1 -1;

    4.Result

  • 相关阅读:
    Gym102028L
    CF985G
    三元环 & 四元环计数 学习笔记
    Hall 定理 学习笔记
    CF36E
    CF1110G
    P6071
    可持久化数据结构 学习笔记
    多项式全家桶
    c++ 编译zlib
  • 原文地址:https://www.cnblogs.com/Fred1987/p/16275149.html
Copyright © 2020-2023  润新知