MySql C ++连接器getString()不能正常工作,而getInt工作完美 [英] MySql C++ connector getString() doesn't work correctly, while getInt works perfectly

查看:461
本文介绍了MySql C ++连接器getString()不能正常工作,而getInt工作完美的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接C ++到MySql,我只是不能让这个工作正常。我从Oracle网站使用了最新的MySql和C ++ / Connector for Windows。我也使用VS2010。



它编译和一切工作除了getString!我用他们提供一些调整的例子:

  #includestdafx.h


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

#include< driver / mysql_public_iface.h>

#includeexamples.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()返回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();
/ *使用驱动程序创建连接* /
std :: auto_ptr< sql :: Connection> con(driver-> connect(localhost,root,root));

/ *创建一个简单语句 - simple=不是准备语句* /
std :: auto_ptr< sql :: Statement> stmt(con> createStatement());

/ *创建一个测试表,演示如何使用sql :: Statement.execute()* /
stmt-> execute(USE publications);
cout<< #\t Test table created<< endl;

/ *用数据填充测试表* /

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

{
/ *
运行查询,返回正好一个结果集,如SELECT
存储过程(CALL)可能返回多个结果集
* /
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;

/ *结果集中的行数* /
cout<< #\t\t Number of rows \t;
cout<< res-> rowsCount()=<< res-> rowsCount()<< endl;
if(res-> rowsCount()!= EXAMPLE_NUM_TEST_ROWS){
msg.str();
msg<< 期望< EXAMPLE_NUM_TEST_ROWS<< rows,found< res-> rowsCount();
throw runtime_error(msg.str());
}

/ *获取数据* /
row = 0;
while(res-> next()){
cout< #\t\t Fetching row<<行<< \t;
/ *你可以使用数字偏移... * /
cout<< id =< res-> getInt(id);
/ * ...或用于访问结果的列名称。建议使用后者。 * /
cout<< ,task ='<< res-> getString(task)<< '< endl;
row ++;
system(PAUSE);
}
}

... //因为它打破了这里。



这是输出:

  1 。
#连接器/ C ++连接的基本用法示例..

#创建测试表
#填充测试表
#运行'SELECT id,task FROM to_do_list ORDER BY id ASC'
#行数res-> rowsCount()= 4
#获取行0 id = 3,任务=''
按任意键继续。 。 。
#获取行1 id = 7,任务=''
按任意键继续。 。 。
☼Ñ╤½xR = I> ☼I>任务↕4å@ xQ @☼☻ocalhost½xÇ
'
按任意键继续。 。 。

这是桌子看起来像这样清楚它不能正常工作。我只是不能得到它的字符串varchar(120)从MySql:

  mysql>使用出版物
数据库已更改
mysql>选择id,任务从to_do_list;
+ ---- + --------------------------- +
| id |任务|
+ ---- + --------------------------- +
| 9 |准备微积分|
| 3 |买牛仔裤|
| 8 |购买新规模|
| 7 |购买类的伦理书|
+ ---- + --------------------------- +
集合中的4行(0.00秒)

我试过VS2008,这是我得到的:


msvcp90d.dll!104e8dcb()

[下面的框可能不正确和/或缺失,没有为msvcp90d.dll加载符号]

try_mysql_native.exe!std :: operator<<(std :: basic_ostream& os = {...},const sql :: SQLString& str = {...})196 + 0x1f字节C ++
try_mysql_native.exe!主(INT ARGC = 1,为const char * argv的= 0x003e63e8)线115 +的0xA5字节C ++
try_mysql_native.exe!__ tmainCRTStartup()行586 +的0x19字节ç
try_mysql_native .exe!mainCRTStartup()Line 403 C
kernel32.dll!7c817077()




try_mysql_native.exe:已加载C:\Documents和Settings\Eugene\My Documents\Visual工作室2008\Projects\try_mysql_native\Debug\try_mysql_native .exe',加载符号。
'try_mysql_native.exe':加载'C:\WINDOWS\system32\\\\\\'dll.dll'
'try_mysql_native.exe':加载'C:\WINDOWS\system32\kernel32。 dll'
'try_mysql_native.exe':Loaded'C:\WINDOWS\system32\mysqlcppconn.dll'
'try_mysql_native.exe':Loaded'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':Loaded' 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':Loaded'C:\WINDOWS\ system32 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\''''
'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'
第一次机会异常在0x104e8dcb在try_mysql_native.exe:0000005:访问冲突读取位置0x6e61656a。
未处理的异常在0x104e8dcb在try_mysql_native.exe:0xC0000005:访问冲突读取位置0x6e61656a。
在0x104e8dcb在try_mysql_native.exe中的第一次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
未处理的异常在0x104e8dcb在try_mysql_native.exe:0xC0000005:访问冲突读取位置0x6e61656a。
在0x104e8dcb在try_mysql_native.exe中的第一次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
未处理的异常在0x104e8dcb在try_mysql_native.exe:0xC0000005:访问冲突读取位置0x6e61656a。
在0x104e8dcb在try_mysql_native.exe中的第一次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
未处理的异常在0x104e8dcb在try_mysql_native.exe:0xC0000005:访问冲突读取位置0x6e61656a。
在0x104e8dcb在try_mysql_native.exe中的第一次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
未处理的异常在0x104e8dcb在try_mysql_native.exe:0xC0000005:访问冲突读取位置0x6e61656a。
在0x104e8dcb在try_mysql_native.exe中的第一次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
未处理的异常在0x104e8dcb在try_mysql_native.exe:0xC0000005:访问冲突读取位置0x6e61656a。
在0x104e8dcb在try_mysql_native.exe中的第一次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
未处理的异常在0x104e8dcb在try_mysql_native.exe:0xC0000005:访问冲突读取位置0x6e61656a。
在0x104e8dcb在try_mysql_native.exe中的第一次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
未处理的异常在0x104e8dcb在try_mysql_native.exe:0xC0000005:访问冲突读取位置0x6e61656a。

解决方案

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



更新



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


I tried to connect C++ to MySql and I just can't get this to work properly. I used latest MySql and C++/Connector for Windows from Oracle site. I also use VS2010.

It compiles and everything works EXCEPT getString! I used the example that they supply with some tweaking:

#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");
        }
    }

... //the rest isn't relevant because it breaks here.

This is the output:

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 . . .

and this is what the table looks like so clearly it's not working properly. I just can't get it to get the strings which are varchar(120) from MySql:

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)

I tried VS2008 and this is what I get:

msvcp90d.dll!104e8dcb()
[Frames below may be incorrect and/or missing, no symbols loaded for msvcp90d.dll]
try_mysql_native.exe!std::operator<<(std::basic_ostream > & os={...}, const sql::SQLString & str={...}) Line 196 + 0x1f bytes C++ try_mysql_native.exe!main(int argc=1, const char * * argv=0x003e63e8) Line 115 + 0xa5 bytes C++ try_mysql_native.exe!__tmainCRTStartup() Line 586 + 0x19 bytes C try_mysql_native.exe!mainCRTStartup() Line 403 C kernel32.dll!7c817077()

and this:

'try_mysql_native.exe': Loaded 'C:\Documents and Settings\Eugene\My Documents\Visual Studio 2008\Projects\try_mysql_native\Debug\try_mysql_native.exe', Symbols loaded. 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\mysqlcppconn.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\libmysql.dll', Symbols loaded. 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\secur32.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\ws2_32.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\ws2help.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.4974_x-ww_d889290f\msvcp90.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.4974_x-ww_d889290f\msvcr90.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcp90d.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\mswsock.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\dnsapi.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\winrnr.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\wldap32.dll' 'try_mysql_native.exe': Loaded 'C:\Program Files\Bonjour\mdnsNSP.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\iphlpapi.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\user32.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\imm32.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\rasadhlp.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\hnetcfg.dll' 'try_mysql_native.exe': Loaded 'C:\WINDOWS\system32\wshtcpip.dll' First-chance exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. Unhandled exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. First-chance exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. Unhandled exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. First-chance exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. Unhandled exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. First-chance exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. Unhandled exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. First-chance exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. Unhandled exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. First-chance exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. Unhandled exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. First-chance exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. Unhandled exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. First-chance exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a. Unhandled exception at 0x104e8dcb in try_mysql_native.exe: 0xC0000005: Access violation reading location 0x6e61656a.

解决方案

Are you sure task-column doesn't have the binary collation set? If so, the connector may return different metadata for the same column.

Update:

Also, check that the Connector has been compiled with the same runtime as your project. If using VS, I bet the other one was compiled with /MT and the other with /MD. This way, they use different heaps and it won't work.

这篇关于MySql C ++连接器getString()不能正常工作,而getInt工作完美的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆