• mysql connector c++插入数据


    /*!
    * Email: scictor@gmail.com
    * Auth:  scictor
    * Date:  2020-05-02 22:26:18
    * File:  mysqlInsertT.cpp
    * Class: %{Cpp:License:ClassName} (if applicable)
    * variable:
    * Brief:
    * Note:
     */
    
    #include <stdio.h>
    
    #include <stdlib.h>
    #include <iostream>
    /*
    Include directly the different
    headers from cppconn/ and mysql_driver.h + mysql_util.h
    (and mysql_connection.h). This will reduce your build time!
    */
    //#include "mysql_connection.h"
    #include <cppconn/driver.h>
    #include <cppconn/exception.h>
    #include <cppconn/resultset.h>
    #include <cppconn/statement.h>
    #include <cppconn/prepared_statement.h>
    
    using namespace std;
    int main(void)
    {
        cout << endl;
        cout << "Let's have MySQL count from 10 to 1..." << endl;
        try {
            sql::Driver *driver;
            sql::Connection *con;
    //        sql::Statement *stmt;
    //        sql::ResultSet *res;
            sql::PreparedStatement *pstmt;
            /* Create a connection */
            driver = get_driver_instance();
            con = driver->connect("tcp://raspberrypi:3306", "root", "root");
            /* Connect to the MySQL test database */
            con->setSchema("test");
    //        stmt = con->createStatement();
    //        stmt->execute("DROP TABLE IF EXISTS test");
    //        stmt->execute("CREATE TABLE test(id INT)");
    //        delete stmt;
            /* '?' is the supported placeholder syntax */
            pstmt = con->prepareStatement("INSERT INTO test(`book`, `list`) VALUES (?, ?)");
            pstmt->setString(1, "AI ACTION");
            pstmt->setString(2, "1000");
            int ret = pstmt->executeUpdate();
            cout << "insert ret:" << ret << endl;
            delete pstmt;
            /* Select in ascending order */
            /*pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");
            res = pstmt->executeQuery();
            //Fetch in reverse = descending order!
            /*res->afterLast();
            while (res->previous())
                cout << "	... MySQL counts: " << res->getInt("id") << endl;
            delete res;*/
    //        delete pstmt;
            delete con;
        } catch (sql::SQLException &e) {
            cout << "# ERR: SQLException in " << __FILE__;
            cout << "(" << __FUNCTION__ << ") on line "
                 << __LINE__ << endl;
            cout << "# ERR: " << e.what();
            cout << " (MySQL error code: " << e.getErrorCode();
            cout << ", SQLState: " << e.getSQLState() <<
                    " )" << endl;
        }
        cout << endl;
        return EXIT_SUCCESS;
    }
  • 相关阅读:
    sqlite数据库的基本操作
    java-正则表达式判断移动联通电信手机号
    基金新手常识
    Android 心跳包心跳连接 如何实现android和服务器长连接呢?推送消息的原理
    java 设计模式之模板方法
    Android 自定义view实现水波纹效果
    Android中pendingIntent的深入理解
    CentOS 7.X下 -- 配置nginx正向代理支持https
    自动化运维工具saltstack04 -- 之jinja模板
    自动化运维工具saltstack03 -- 之SaltStack的数据系统
  • 原文地址:https://www.cnblogs.com/guxuanqing/p/12820017.html
Copyright © 2020-2023  润新知