【问题标题】:MySql C++ connector getString() doesn't work correctly, while getInt works perfectlyMySql C++ 连接器 getString() 不能正常工作,而 getInt 工作正常
【发布时间】:2011-06-16 21:58:14
【问题描述】:

我尝试将 C++ 连接到 MySql,但我无法让它正常工作。我使用了来自 Oracle 站点的最新的 MySql 和 C++/Connector for Windows。我也用VS2010。

它编译并且一切正常,除了getString!我使用了他们提供的示例并进行了一些调整:

#include "stdafx.h"


#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>

#include <driver/mysql_public_iface.h>

#include "examples.h"

using namespace std;

int main(int argc, const char **argv)
{
string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);

/* sql::ResultSet.rowsCount() returns size_t */
size_t row;
stringstream sql;
stringstream msg;
int i, affected_rows;

cout << boolalpha;
cout << "1..1" << endl;
cout << "# Connector/C++ connect basic usage example.." << endl;
cout << "#" << endl;

try {
    sql::Driver * driver = sql::mysql::get_driver_instance();
    /* Using the Driver to create a connection */
    std::auto_ptr< sql::Connection > con(driver->connect("localhost", "root", "root"));

    /* Creating a "simple" statement - "simple" = not a prepared statement */
    std::auto_ptr< sql::Statement > stmt(con->createStatement());

    /* Create a test table demonstrating the use of sql::Statement.execute() */
    stmt->execute("USE publications");
    cout << "#\t Test table created" << endl;

    /* Populate the test table with data */

    cout << "#\t Test table populated" << endl;

    {
        /*
        Run a query which returns exactly one result set like SELECT
        Stored procedures (CALL) may return more than one result set
        */
        std::auto_ptr< sql::ResultSet > res(stmt->executeQuery("SELECT id, task FROM to_do_list ORDER BY id ASC"));
        cout << "#\t Running 'SELECT id, task FROM to_do_list ORDER BY id ASC'" << endl;

        /* Number of rows in the result set */
        cout << "#\t\t Number of rows\t";
        cout << "res->rowsCount() = " << res->rowsCount() << endl;
        if (res->rowsCount() != EXAMPLE_NUM_TEST_ROWS) {
            msg.str("");
            msg << "Expecting " << EXAMPLE_NUM_TEST_ROWS << "rows, found " << res->rowsCount();
            throw runtime_error(msg.str());
        }

        /* Fetching data */
        row = 0;
        while (res->next()) {
            cout << "#\t\t Fetching row " << row << "\t";
            /* You can use either numeric offsets... */
            cout << "id = " << res->getInt("id");
            /* ... or column names for accessing results. The latter is recommended. */
            cout << ", task = '" << res->getString("task") << "'" << endl;
            row++;
            system("PAUSE");
        }
    }

... //其余的不相关,因为它在这里中断了。

这是输出:

1..1
# Connector/C++ connect basic usage example..
#
#        Test table created
#        Test table populated
#        Running 'SELECT id, task FROM to_do_list ORDER BY id ASC'
#                Number of rows res->rowsCount() = 4
#                Fetching row 0 id = 3, task = ''
Press any key to continue . . .
#                Fetching row 1 id = 7, task = ''
Press any key to continue . . .
   ☼   Ñ╤½x R=  I>             ☼   £I> task  ↕ 4å@ xQ@ ♦   ☼   ☻    ocalhost ½xÇ
'
Press any key to continue . . .

这就是桌子的样子,很明显它不能正常工作。我只是无法从 MySql 中获取 varchar(120) 的字符串:

mysql> use publications
Database changed
mysql> select id, task from to_do_list;
+----+---------------------------+
| id | task                      |
+----+---------------------------+
|  9 | prepare for calculus      |
|  3 | buy jeans                 |
|  8 | buy new scale             |
|  7 | buy Ethics book for class |
+----+---------------------------+
4 rows in set (0.00 sec)

我尝试了 VS2008,这是我得到的:

msvcp90d.dll!104e8dcb()
[以下帧可能不正确和/或丢失,没有为 msvcp90d.dll 加载符号]
try_mysql_native.exe!std::operator & os={...}, const sql::SQLString & str={...}) 第 196 行 + 0x1f 字节 C++ try_mysql_native.exe!main(int argc=1, const char * * argv=0x003e63e8) 第 115 行 + 0xa5 字节 C++ try_mysql_native.exe!__tmainCRTStartup() 第 586 行 + 0x19 字节 C try_mysql_native.exe!mainCRTStartup() 第 403 行 C kernel32.dll!7c817077()

还有这个:

'try_mysql_native.exe':已加载'C:\Documents and Settings\Eugene\My Documents\Visual Studio 2008\Projects\try_mysql_native\Debug\try_mysql_native.exe',已加载符号。 “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\ntdll.dll” “try_mysql_native.exe”:加载“C:\WINDOWS\system32\kernel32.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\mysqlcppconn.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\libmysql.dll”,已加载符号。 “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\advapi32.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\rpcrt4.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\secur32.dll” “try_mysql_native.exe”:加载“C:\WINDOWS\system32\ws2_32.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\msvcrt.dll” “try_mysql_native.exe”:加载“C:\WINDOWS\system32\ws2help.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.4974_x-ww_d889290f\msvcp90.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.4974_x-ww_d889290f\msvcr90.dll” 'try_mysql_native.exe': 加载'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll' “try_mysql_native.exe”:已加载“C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcp90d.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\mswsock.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\dnsapi.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\winrnr.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\wldap32.dll” “try_mysql_native.exe”:已加载“C:\Program Files\Bonjour\mdnsNSP.dll” “try_mysql_native.exe”:加载“C:\WINDOWS\system32\iphlpapi.dll” “try_mysql_native.exe”:加载“C:\WINDOWS\system32\user32.dll” “try_mysql_native.exe”:加载“C:\WINDOWS\system32\gdi32.dll” “try_mysql_native.exe”:加载“C:\WINDOWS\system32\imm32.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\rasadhlp.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\hnetcfg.dll” “try_mysql_native.exe”:已加载“C:\WINDOWS\system32\wshtcpip.dll” try_mysql_native.exe 中 0x104e8dcb 的第一次机会异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 处的未处理异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 的第一次机会异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 处的未处理异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 的第一次机会异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 处的未处理异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 的第一次机会异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 处的未处理异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 的第一次机会异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 处的未处理异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 的第一次机会异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 处的未处理异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 的第一次机会异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 处的未处理异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 的第一次机会异常:0xC0000005:访问冲突读取位置 0x6e61656a。 try_mysql_native.exe 中 0x104e8dcb 处的未处理异常:0xC0000005:访问冲突读取位置 0x6e61656a。

【问题讨论】:

  • 使用 MySQL Server 5.5 MySQL Connector C++ 1.1.0。

标签: c++ mysql database-connection


【解决方案1】:

我在环境“mysql-connector-c++-8.0.13-win32”和Visual C++ 2015中试运行MySQL Connector/C++ Developer Guide / Getting Started with Connector/C++: Usage Examples / Complete Example 1的demo源码。

我也遇到了同样的问题。

在调试模式下,应该使用

std::string version = result->getString( COLUMN_NAME ).c_str();

但在发布模式下,这是可以的:

sql::SQLString sString  = result->getString( COLUMN_NAME );

我相信这是因为我的项目必须使用与 MySQL C++ 连接器库相同的运行时进行编译。

【讨论】:

    【解决方案2】:

    下面的代码对我有用:

    sql::ResultSet *res;
    res->getString("Column name").c_str();
    

    只是 using res-&gt;getString("Column name") 使我的应用程序崩溃。

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题,程序会给出内存异常。这是我为修复它所做的:

      std::string version = result->getString( COLUMN_NAME ).c_str();
      

      这不起作用:

      sql::SQLString sString  = result->getString( COLUMN_NAME ); <<<memory exception
      std::string version = sString; 
      

      【讨论】:

      • 我知道这有点旧,但这确实帮助了我。我想知道为什么它会起作用。
      【解决方案4】:

      您确定 task-column 没有设置二进制排序规则吗?如果是这样,连接器可能会为同一列返回不同的元数据。

      更新:

      此外,请检查连接器是否已使用与您的项目相同的运行时进行编译。如果使用 VS,我敢打赌另一个是用 /MT 编译的,另一个是用 /MD 编译的。这样一来,他们使用不同的堆,这将不起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-13
        • 1970-01-01
        相关资源
        最近更新 更多