Hello all!
I am trying to create a simple app that takes something from an mysql database and prints it, I had some problems with the libraries at first, but now it compiles and shows me the black console window, but then suddenly it gives me some errors and in the callstack it says:
Mysql.exe!main(int argc=1, char ** argv=0x00392700) Line 31 + 0x9bytes.
The error popup says:
Unhandled exception at 0x002a7384 in Mysql.exe: 0xC0000005: Access violation reading location 0x00000000.
I am new to C++ and to be honest I do not have any idea on what the hell is going, could someone please help me out?
Thanks in advance for any replies, Anton
Here's the code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <my_global.h>
#include <mysql.h>
#pragma comment(lib, "mysqlclient.lib") // this too, although it seems you got these
#pragma comment(lib, "libmysql.lib") // and this too figured
#pragma comment(lib, "mysys.lib") // mandatory out
#define host "localhost"
#define username "****"
#define password "****"
#define database "test"
using namespace std;
int main(int argc, char* argv[])
{
MYSQL* conn;
conn = mysql_init( NULL );
if( conn )
{
mysql_real_connect( conn, host, username, password, database, 0, NULL, 0 );
}
MYSQL_RES* res_set;
MYSQL_ROW row;
unsigned int i;
mysql_query( conn, "SELECT * FROM animals WHERE id = 1" );
res_set = mysql_store_result( conn );
unsigned int numrows = mysql_num_rows( res_set );
if( numrows )
{
row = mysql_fetch_row( res_set );
if( row != NULL )
{
cout << "Animal ID : " << row[0] << endl;
cout << "Animal Name: " << row[1] << endl;
}
}
if( res_set )
{
mysql_free_result( res_set );
}
if( conn )
{
mysql_close( conn );
}
return 0;
}