943,073 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 360
  • C++ RSS
Jul 28th, 2010
0

Mysql in C++

Expand Post »
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:
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <my_global.h>
  5. #include <mysql.h>
  6.  
  7. #pragma comment(lib, "mysqlclient.lib") // this too, although it seems you got these
  8. #pragma comment(lib, "libmysql.lib") // and this too figured
  9. #pragma comment(lib, "mysys.lib") // mandatory out
  10.  
  11. #define host "localhost"
  12. #define username "****"
  13. #define password "****"
  14. #define database "test"
  15.  
  16. using namespace std;
  17.  
  18. int main(int argc, char* argv[])
  19. {
  20. MYSQL* conn;
  21. conn = mysql_init( NULL );
  22. if( conn )
  23. {
  24. mysql_real_connect( conn, host, username, password, database, 0, NULL, 0 );
  25. }
  26. MYSQL_RES* res_set;
  27. MYSQL_ROW row;
  28. unsigned int i;
  29. mysql_query( conn, "SELECT * FROM animals WHERE id = 1" );
  30. res_set = mysql_store_result( conn );
  31. unsigned int numrows = mysql_num_rows( res_set );
  32. if( numrows )
  33. {
  34. row = mysql_fetch_row( res_set );
  35. if( row != NULL )
  36. {
  37. cout << "Animal ID : " << row[0] << endl;
  38. cout << "Animal Name: " << row[1] << endl;
  39. }
  40. }
  41. if( res_set )
  42. {
  43. mysql_free_result( res_set );
  44. }
  45. if( conn )
  46. {
  47. mysql_close( conn );
  48. }
  49.  
  50. return 0;
  51. }
Last edited by scar164; Jul 28th, 2010 at 2:11 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scar164 is offline Offline
5 posts
since Jul 2010
Jul 28th, 2010
0
Re: Mysql in C++
<deleted>
Last edited by Ancient Dragon; Jul 28th, 2010 at 2:51 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,934 posts
since Aug 2005
Jul 28th, 2010
0
Re: Mysql in C++
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?

c++ Syntax (Toggle Plain Text)
  1. #include <my_global.h>
  2. #include <mysql.h>
  3.  
  4. #pragma comment(lib, "mysqlclient.lib") // this too, although it seems you got these
  5. #pragma comment(lib, "libmysql.lib") // and this too figured
  6. #pragma comment(lib, "mysys.lib") // mandatory out
  7.  
  8. int main(int argc, char **argv)
  9. {
  10.  
  11. MYSQL *conn;
  12.  
  13. conn = mysql_init(NULL);
  14. mysql_real_connect(conn, "localhost", "zetcode", "passwd", "testdb", 3306, NULL, 0);
  15.  
  16. mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25))");
  17.  
  18. mysql_query(conn, "INSERT INTO writers VALUES('Leo Tolstoy')");
  19. mysql_query(conn, "INSERT INTO writers VALUES('Jack London')");
  20. mysql_query(conn, "INSERT INTO writers VALUES('Honore de Balzac')");
  21. mysql_query(conn, "INSERT INTO writers VALUES('Lion Feuchtwanger')");
  22. mysql_query(conn, "INSERT INTO writers VALUES('Emile Zola')");
  23.  
  24. mysql_close(conn);
  25.  
  26. }
Last edited by scar164; Jul 28th, 2010 at 3:05 pm. Reason: added port to mysql_real_connect, it still doesn't work though
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scar164 is offline Offline
5 posts
since Jul 2010
Jul 28th, 2010
0
Re: Mysql in C++
If the table has two or more columns then you have to specify the column names add values for all columns.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,934 posts
since Aug 2005
Jul 28th, 2010
0
Re: Mysql in C++
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:
c++ Syntax (Toggle Plain Text)
  1. for(i = 0; i < num_fields; i++)
  2. {
  3. printf("%s ", row[i] ? row[i] : "NULL");
  4. string name = row[1];
  5. width = atoi(row[2]);
  6. height = atoi(row[3]);
  7. CRectangle name ( width, height);
  8. cout << "rect area: " << name.area() << endl;
  9. }

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:
C++ Syntax (Toggle Plain Text)
  1. 1>c:\users\anton\documents\visual studio 2008\projects\mysq\mysq\main.cpp(61) : error C2371: 'name' : redefinition; different basic types
  2. 1> c:\users\anton\documents\visual studio 2008\projects\mysq\mysq\main.cpp(58) : see declaration of 'name'
  3. 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>'
  4. 1> with
  5. 1> [
  6. 1> _Elem=char,
  7. 1> _Traits=std::char_traits<char>,
  8. 1> _Ax=std::allocator<char>
  9. 1> ]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scar164 is offline Offline
5 posts
since Jul 2010
Jul 28th, 2010
0
Re: Mysql in C++
my guess is that you have already used the symbol "name" somewhere eariler in your program.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,934 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ book recommendations
Next Thread in C++ Forum Timeline: BigInt multiplication & handling positive/negative





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC