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;
}

Edited 6 Years Ago by scar164: n/a

I've had a look at this website and tried to run the code below, with my database connection. At least now it runs, but when checking my database, nothing was created, why is that?

I know this might not have anything to do with my errors above in the initial code, but at least this shows that it will not work anyway, or?

#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

int main(int argc, char **argv)
{

  MYSQL *conn;

  conn = mysql_init(NULL);
  mysql_real_connect(conn, "localhost", "zetcode", "passwd", "testdb", 3306, NULL, 0);

  mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25))");

  mysql_query(conn, "INSERT INTO writers VALUES('Leo Tolstoy')");
  mysql_query(conn, "INSERT INTO writers VALUES('Jack London')");
  mysql_query(conn, "INSERT INTO writers VALUES('Honore de Balzac')");
  mysql_query(conn, "INSERT INTO writers VALUES('Lion Feuchtwanger')");
  mysql_query(conn, "INSERT INTO writers VALUES('Emile Zola')");

  mysql_close(conn);

}

Edited 6 Years Ago by scar164: added port to mysql_real_connect, it still doesn't work though

Well it works now, I just played around with the examples on the webpage I linked and got it to work, thanks for pointing me in the right direction Ancient Dragon. Now though, I am trying to create a class named with the value from table animals. My english isn't that good, but I hope you understand what I mean.

This is my for loop:

for(i = 0; i < num_fields; i++)
		  {
			  printf("%s ", row[i] ? row[i] : "NULL");
			  string name = row[1];
			  width = atoi(row[2]);
			  height = atoi(row[3]);
			  CRectangle name ( width, height);
			  cout << "rect area: " << name.area() << endl;
		  }

It worked just a second ago to print out everything inside of the table animals, but when trying to create CRectangle using name, it gives me theese two errors:

1>c:\users\anton\documents\visual studio 2008\projects\mysq\mysq\main.cpp(61) : error C2371: 'name' : redefinition; different basic types
1>        c:\users\anton\documents\visual studio 2008\projects\mysq\mysq\main.cpp(58) : see declaration of 'name'
1>c:\users\anton\documents\visual studio 2008\projects\mysq\mysq\main.cpp(62) : error C2039: 'area' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
This question has already been answered. Start a new discussion instead.