MySQL Connector/C++ 开发者指南简要阅读

本文简要介绍了MySQL Connector/C++的开发者指南,特别提到存储过程如何通过设置输出参数值而非直接返回结果,以及如何处理返回的多个结果集。
摘要由CSDN通过智能技术生成
一: 源代码目录的示例文件解释
    在MySQL Connector/C++的源代码目录中有一个examples文件夹,这里包含了很多使用例子。这些例子演示了如何使用这些类:Connection、Driver、PreparedStatement、
ResultSet、ResultSetMetaData、Statement。这些例子都不是完整的例子,只是一些代码框架,你可以直接拿来使用。
    这些例子包含以下内容
        使用Driver类连接MySQL。
        使用(simple) statements创建表、插入记录、从数据库记录。
        使用prepared statements创建表、插入记录、从数据库记录。
        声明的一些建议。
         访问结果集元数据。
examples/connect.cpp:    
    该文件演示了如何创建连接、如何向MySQL中插入数据、如何获取异常。
examples/connection_meta_schemaobj.cpp
    演示怎么从connection对象获取元数据,如表列表、数据库列表、MySQL的版本号,connection的版本号
examples/debug_output.cpp
    演示如何激活和停用Connector/C++的调试协议
examples/exceptions.cpp
    演示如何查看 Connector/C++抛出的异常以及如何捕获它。
examples/prepared_statements.cpp:
    How to run Prepared Statements including an example how to handle SQL statements that cannot be   prepared by the MySQL Server
examples/resultset.cpp:
    How to use a cursor to fetch data and iterate over a result set.
examples/resultset_meta.cpp:
    How to obtain metadata associated with a result set, for example, number of columns and column types
examples/resultset_types.cpp:
    Result sets returned from metadata methods. (This is more a test than an example.)
examples/standalone_example.cpp:
    Simple standalone program not integrated into regular CMake builds.
examples/statements.cpp:
    How to execute SQL statements without using Prepared Statements.
examples/cpp_trace_analyzer.cpp:
    This example shows how to filter the output of the debug trace. Please see the inline comments for   further documentation. This script is unsupported.
二:如何使用Connector/C++连接MySQL
    sql::mysql::MySQL_Driver *driver;
    sql::Connection *con;
    driver = sql::mysql::MySQL_Driver::get_mysql_driver_instance();
    con = driver->connect("tcp://127.0.0.1:3306", "user", "password");
    delete con;
如果不在使用com对象,就需要及时释放该对象。但是不用显示释放driver对象。
    sql::Connection::isValid()            检查连接释放激活
    sql::Connection::reconnect()       重新连接
范例1 普通范例
    sql::mysql::MySQL_Driver *driver;
    sql::Connection *con;
    sql::Statement *stmt;
    driver = sql::mysql::get_mysql_driver_instance();
    con = driver->connect("tcp://127.0.0.1:3306", "user", "password");
    stmt = con->createStatement();
    stmt->execute("USE " EXAMPLE_DB);
    stmt->execute("DROP TABLE IF EXISTS test");
    stmt->execute("CREATE TABLE test(id INT, label CHAR(1))");
    stmt->execute("INSERT INTO test(id, label) VALUES (1, 'a')");
    delete stmt;
    delete con;
    请注意必须显示的释放 sql::Statement对象和sql::Connection对象。
范例2 获取查询结果
    // ...
    sql::Connection *con;
    sql::Statement *stmt;
    sql::ResultSet *res;
    // ...
    stmt = con->createStatement();
    // ...
    res = stmt->executeQuery("SELECT id, label FROM test ORDER BY id ASC");
    while (res->next()) {
        // You can use either numeric offsets...
        cout << "id = " << res->getInt(1); // getInt(1) returns the first column
        // ... or column names for accessing results.
        // The latter is recommended.
        cout << ", label = '" << res->getString("label") << "'" << endl;
    }
    delete res;
    delete stmt;
三:使用Using Prepared Statements
    // ...
    sql::Connection *con;
    sql::PreparedStatement *prep_stmt
    // ...
    prep_stmt = con->prepareStatement("INSERT INTO test(id, label) VALUES (?, ?)");
    prep_stmt->setInt(1, 1);
    prep_stmt->setString(2, "a");
    prep_stmt->execute();
    prep_stmt->setInt(1, 2);
    prep_stmt->setString(2, "b");
    prep_stmt->execute();
    delete prep_stmt;
    delphi con;
四:使用Connector/C++连接mysql的简单完整示例1
    /* Standard C++ includes */
    #include <stdlib.h>
    #include <iostream>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值