• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

mysqlconnectorc++插入数据

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

/*!
* Email: [email protected]
* 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 << "\t... 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;
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#变量中的问号发布时间:2022-07-13
下一篇:
十个例子让你完全搞懂C++的虚函数(不懂来找我)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap