MySQL Connector / C ++代码在行崩溃:std :: cout&lt; <res-> getString(“label”); </res->

时间:2013-08-02 03:26:01

标签: c++ visual-studio-2010 mysql-connector

我知道在这个论坛中有很多关于如何解决这个问题的建议,但没有一个能解决我的问题。

它总是一样的:代码编译得很好,我可以使用ResultSet提供的几乎所有getXX成员,除了getString,它也可以毫无问题地编译,但总是在运行时崩溃。

我做了一些奇怪的尝试,比如使用getInt(“label”)或getBlob(“label”)获取char字段,结果并不像我之前所知的那样好,但至少我没有遇到崩溃

我尝试了版本1.0.5和1.1.3 + boost。在1.1.3的情况下,我也使用CMake从源代码编译,但我认为我对该实用程序的知识是不充分的,并且在我控制CMake之前会持续很长时间。所以我想得到一个更直接的解决方案。

我的目标是从MSVC 2010创建一些Win32_Console和WinForms代码,这些代码在我的网络的XUbuntu机器上运行的MySQL 5.6中读写数据。并且getString()正在困扰我。

提前致谢

我在下面添加了代码:

#include "stdafx.h"
#include <iostream>

#include <mysql_driver.h>
#include <mysql_connection.h>
#include <exception.h>
#include <statement.h>
#include <resultset.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
cout<<"Testando uma conexao...\n";
cout<<"Connector 1.1.3:\n";
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
driver = sql::mysql::get_mysql_driver_instance();

try
{
     con = driver->connect("tcp://192.168.1.75:3306", "site_rw", "");
     if (con->isClosed()==0)
      cout<<"Sucesso!\n\n";

     cout<<"Inserindo statement...\n";
     stmt = con->createStatement();
     stmt->execute("USE test_area"); 
     stmt->execute("DROP TABLE IF EXISTS test");
     stmt->execute("CREATE TABLE test(id INT, label CHAR(1))");
     cout<<"Criada a tabela...\n";
     for (int k=0; k<30;k++)
     stmt->execute("INSERT INTO test(id, label) VALUES (1, 'a')");
     cout<<"Linhas inseridas...\n";
     cout<<"Preparando-se para listar os inteiros:\n";

     res = stmt->executeQuery("SELECT * FROM test LIMIT 0,5");
     while (res->next())
       cout<<"id = " << res->getInt("id") << "\n";

     cout<<"\n\nAgora tentando o char(1):\n";
     res->beforeFirst();
     while (res->next())
           cout<<"label = " << res->getString("label");


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() << endl;
  cout << " (MySQL error code: " << e.getErrorCode() << endl;
  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}

cout<<"Terminado...\n";

return 0;

}

Aluísio 圣保罗

0 个答案:

没有答案