zoukankan      html  css  js  c++  java
  • 使用Connector/C++(VS2015)连接MySQL的完整例子

    完整示例代码1

    /* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 of the License.
    There are special exceptions to the terms and conditions of the GPL
    as it is applied to this software. View the full text of the
    exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
    software distribution.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    */
    /* Standard C++ includes */
    #include "mysql_connection.h"
    #include <cppconn/driver.h>
    #include <cppconn/exception.h>
    #include <cppconn/resultset.h>
    #include <cppconn/statement.h>
    using namespace std;
    int main(void)
    {
        cout << endl;
        cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;
            try {
            sql::Driver *driver;
            sql::Connection *con;
            sql::Statement *stmt;
            sql::ResultSet *res;
            /* Create a connection */
            driver = get_driver_instance();
            con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
            /* Connect to the MySQL test database */
            con->setSchema("test");
            stmt = con->createStatement();
            res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
            while (res->next()) {
                cout << "	... MySQL replies: ";
                /* Access column data by alias or column name */
                cout << res->getString("_message") << endl;
                cout << "	... MySQL says it again: ";
                /* Access column data by numeric offset, 1 is the first column */
                cout << res->getString(1) << endl;
            }
            delete res;
            delete stmt;
            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;
    }

    完整示例代码2:如何使用Connector/C++的完整例子

    /* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 of the License.
    There are special exceptions to the terms and conditions of the GPL
    as it is applied to this software. View the full text of the
    exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
    software distribution.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    */
    /* Standard C++ includes */
    
    #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://127.0.0.1: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(id) VALUES (?)");
            for (int i = 1; i <= 10; i++) {
                pstmt->setInt(1, i);
                pstmt->executeUpdate();
            }
            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;
    }
  • 相关阅读:
    【硬件】组装一台多核电脑
    【硬件】组装电脑前的准备工作
    【长知识】设计多核电脑装机方案
    【长知识】认识电脑的硬件组成
    程序员必备基础:如何安全传输存储用户密码?
    二本应届生的大学生活、2020年总结(已上岸百度)
    白日梦的Elasticsearch系列笔记(一)基础篇-- 快手上手ES
    全网最牛X的!!! MySQL两阶段提交串讲
    删库后!除了跑路还能干什么?
    数据库面试简答、30道高频面试题
  • 原文地址:https://www.cnblogs.com/mobilecard/p/9196213.html
Copyright © 2011-2022 走看看