2
#include "StdAfx.h"
#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>
#include <cppconn/prepared_statement.h>

using namespace std;

int main(void){
    cout << endl;

    try{
        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        sql::ResultSet *res;
        sql::PreparedStatement *pstmt;

        driver = get_driver_instance();
        con = driver->connect("REMOVED", "REMOVED", "REMOVED");
        con->setSchema("REMOVED");

        stmt = con->createStatement();
        res = stmt->executeQuery("SELECT username FROM player WHERE id=1");

        cout << "Username: " << res->getString("username") << endl;

        delete res;
        delete con;

        cout << "Done.";
        system("pause");

    }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() << endl;
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
        system("pause");
    }

    return 0;
}

turns into... https://i.stack.imgur.com/URGiu.png

What's happening? :( This is only my second day working with C++, so forgive my terribly formatted coding and other nooby mistakes. This always shows up right before I get an unhandled exception.

Jörg Beyer
  • 3,631
  • 21
  • 35
  • The output you're getting looks like what happens when you output unitialized memory on a Windows system in debug mode. – Max Lybbert Mar 06 '12 at 20:33
  • For what it's worth, the `__FILE__`, `__FUNCTION__` and `__LINE__` macros will expand out to the filename, function name and line number of the `catch` clause, not the filename, function name and line number where the exception itself was thrown. I'm not sure if that's the information you actually want. i.e., you will get something like `ERR: SQLException in foo.cpp (main) on line 42 ...` – Max Lybbert Mar 06 '12 at 20:37
  • @MaxLybbert Thank you, I had no idea! I appreciate the information, I'm sure I would have been perplexed by that at some point in the future. – user1253114 Mar 06 '12 at 20:44

1 Answers1

0

The APIs you are using return std::string objects I believe.

In this case you probably want to add a #include <sstream> to the top of your code so that you have well a defined '<<' operator for the std::string class.

I'm not certain of this solution because I would have expected you to get a compile error that somehow hinted that no operator '<<' could be found.

Try sticking this in your code and calling it from main() right away just to see if the streaming operators are working. Note it will make your program exit.

void SimpleStrings()
{
    std::string str("String in a std::string");
    const char *psz = "const-char-str";
    std::cout << "std::string: " << str << std::endl << "const char *: " << psz << std::endl;
    exit(1);
}
Joe
  • 2,946
  • 18
  • 17
  • I added `#include ` and now it's just looping through hundreds of lines of unintelligible characters before it crashes. I've been playing around with this for several hours now and have been going back and forth with different things - putting the username cout in a `while(res->next()){` loop (and closing it after the username cout) spams me w/ above pictured output, while commenting out the cout and leaving the while loop executes the rest of the code normally. It's just that output. I know this is now extending more into an issue with MySQL though...or perhaps my general newness. :( – user1253114 Mar 06 '12 at 20:47
  • See the edit to my post, try adding the test routine and see if it works ok. – Joe Mar 06 '12 at 20:55
  • "The program '[8680] Test.exe: Native' has exited with code 1 (0x1)." I stumbled across this [link](http://stackoverflow.com/questions/4822958/mysql-c-connector-getstring-doesnt-work-correctly-while-getint-works-perfe) (see accepted answer update) and I'm wondering if that is the issue...except I feel like I'm wandering in a land where I know nothing, absolutely nothing, and don't know what those are and how to approach changing the connector's runtime. I did google and find out how to change the program's runtime - using both /MT and /MD had same results. Would changing the connectors fix it? – user1253114 Mar 06 '12 at 21:11
  • I'm sure it does matter how you link the CRT. When you messed with /MT and /MD you might want to double check you did it right. Visual C will often throw /MDd or /MTd when doing a debug build. Double check by opening the build log link at the end of the build. Not quite as important, but ideally you will be building with the same version of Visual Studio that the connector was built with. Do you have the link for where you got the connector? If so, I can take a peek. – Joe Mar 06 '12 at 23:34
  • I'm not quite sure what I'm supposed to be checking for (sorry, I hate to be _THAT_ person!).. I just started playing around with the different settings and /MTd AND /MDd appears to make everything function 100% correctly. That's just with 2 minutes of testing - no errors, nothing like before. I'd do some more testing but I'm pressed for time right this moment. /MT throws same errors, as does /MD. Connector @ [link](http://www.mysql.com/downloads/connector/cpp/) (_mysql-connector-c++-1.1.0-win32.msi_ first download on that page). I appreciate you taking the time to help me. Thank you! – user1253114 Mar 07 '12 at 00:46
  • Just for extra clarification (ran out of space above) - With /MTd or /MDd on, MySQL works just fine. It's getting the correct value from the database as it should be. – user1253114 Mar 07 '12 at 00:49