#include "stdafx.h" #include #include //Sugestion (http://stackoverflow.com/questions/4225812/c-mysql-sqlstring-crashes-0xc0000005-unable-to-read-memory) #include //#include #include #include #include #include #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string fld="Nada"; cout<<"Connection test using Connector/C++ 1.1.3:\n"; sql::mysql::MySQL_Driver *driver; sql::Connection *con; sql::Statement *stmt; sql::ResultSet *res; sql::SQLString tst, tmp01; sql::SQLString * tmp_sql_string = 0x0; driver = sql::mysql::get_mysql_driver_instance(); try { con = driver->connect("tcp://192.168.1.75:3306", "site_rw", ""); if (con->isClosed()==0) cout<<"Success!\n\n"; cout<<"Inserting statement...\n"; stmt = con->createStatement(); stmt->execute("USE test_area"); stmt->execute("DROP TABLE IF EXISTS test"); stmt->execute("CREATE TABLE test(id INT, perc DOUBLE, completo BOOL, label01 CHAR(5))"); cout<<"Table done...\n"; for (int k=0; k<30;k++) stmt->execute("INSERT INTO test(id, perc, completo, label01) VALUES (1, .93, TRUE, 'Ajkjk')"); cout<<"Table populated...\n"; res = stmt->executeQuery("SELECT * FROM test LIMIT 0,3"); cout<<"\nTotal fields on table: "<getMetaData()->getColumnCount(); cout<<"\nType of *res \t\t\t\t: " <first()).name(); cout<<"\nType *res->getMetaData() \t\t: " <getMetaData()->getColumnName(1)).name(); cout<<"\n\nListing non-char fields:"; while (res->next()) { cout<<"\nid = " <getInt("id")<<"\tperc: "<getDouble("perc")<<"\tcompleto : "<getBoolean("completo"); } res->beforeFirst(); cout<<"\n\nNow trying to list the char field label01;\nHere, in my case, code compiles but crashes at the runtime\n(cursor blinks after first value output):"; while (res->next()) { cout<<"\nlabel01 = "<getString("label01"); } 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<<"\n.\n..\n...\nPassed the try/catch block!!!"; cout<<"\nEnter to terminate"; cin.get(); return 0; }