Re: MySQL, C++, Windows XP: mysql_init() throws an exception
Now try to use "Connector/C++" and the next code:
#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;
//const char* serverAddress = "127.0.0.1:3306";
const char* serverAddress = "localhost";
try
{
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
// Create a connection
driver = get_driver_instance();
printf("driver = %p\n", driver);
char buffer[1024];
sprintf(buffer, serverAddress);
con = driver->connect(buffer, "root", "7777777");
// Connect to the MySQL test database
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next())
{
cout << "\t... MySQL replies: ";
// Access column data by alias or column name
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
// Access column fata by numeric offset, 1 is the first column
cout << res->getString(1) << endl;
}
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();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
getchar();
return 0;
}
fails with exception "Access violation reading location ..." at line "cout << res->getString("_message") << endl;" but console says:
Running 'SELECT 'Hello World!' AS _message'...
driver = 003D4E38
... MySQL replies: Hello World!
Subject
Views
Written By
Posted
1264
December 24, 2010 06:17PM
Re: MySQL, C++, Windows XP: mysql_init() throws an exception
706
December 25, 2010 05:59AM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.