无法从DB读取字符串数据

时间:2014-04-24 21:32:01

标签: c++ mysql connector

我有带连接器c ++ 1.1.3的MySQL 5.6。我的问题是我不能从我的表中显示字符串数据,而INT的数据可以没有任何问题地列出。请看这个:

/* INSERT TUTORIAL CODE HERE! */
sql::Driver *driver = get_driver_instance();
std::auto_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3307", "root", "pass"));
con->setSchema("mybase");
std::auto_ptr<sql::Statement> stmt(con->createStatement());

//example from dev/mysql site

stmt->execute("DROP TABLE IF EXISTS mcountry");
stmt->execute("DROP PROCEDURE IF EXISTS add_ctry");
stmt->execute("DROP PROCEDURE IF EXISTS getdata");

stmt->execute("CREATE TABLE mcountry(country_id INT)");
stmt->execute("CREATE PROCEDURE add_ctry(IN cname INT) BEGIN INSERT INTO mcountry(country_name) VALUES (cname); END;");
stmt->execute("CREATE PROCEDURE getdata() BEGIN SELECT * FROM mcountry; END;");

stmt->execute("CALL add_ctry(112)");
stmt->execute("CALL add_ctry(323)");

stmt->execute("CALL getdata()");

std::auto_ptr<sql::ResultSet> res;

do{
res.reset(stmt->getResultSet());
while(res->next())
{
cout << "ID: " << res->getInt(1);
}
} while(stmt->getMoreResults());

这项工作很棒。问题是当我将变量从INT更改为CHAR(somevalue)时,如下所示:

sql::Driver *driver = get_driver_instance();
std::auto_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3307", "root", "pass"));
con->setSchema("mybase");
std::auto_ptr<sql::Statement> stmt(con->createStatement());

//example from dev/mysql site

stmt->execute("DROP TABLE IF EXISTS mcountry");
stmt->execute("DROP PROCEDURE IF EXISTS add_ctry");
stmt->execute("DROP PROCEDURE IF EXISTS getdata");

stmt->execute("CREATE TABLE mcountry(country_name CHAR(3))");
stmt->execute("CREATE PROCEDURE add_ctry(IN cname CHAR(3)) BEGIN INSERT INTO mcountry(country_name) VALUES (cname); END;");
stmt->execute("CREATE PROCEDURE getdata() BEGIN SELECT * FROM mcountry; END;");

stmt->execute("CALL add_ctry('abc')");
stmt->execute("CALL add_ctry('def')");

stmt->execute("CALL getdata()");

std::auto_ptr<sql::ResultSet> res;

string str;

do{
res.reset(stmt->getResultSet());
while(res->next())
{
//cout << "Name: " << res->getString(1);
  str = res->getString(1);
  cout << str; //now instead of abc and def i get NON ASCI character
}
} while(stmt->getMoreResults());

我收到错误:tstproj.exe中0x70481174处的未处理异常:0xC0000005:访问冲突读取位置0x00221000。

VS2010将我重定向到字符串标准标题,并指示:

if (_State == ios_base::goodbit
            && _Ostr.rdbuf()->sputn(_Str.c_str(), _Size) != (streamsize)_Size)
                _State |= ios_base::badbit;

感谢您的回答。

0 个答案:

没有答案