Advertisement

connection mysql database

Started by December 08, 2005 09:36 AM
11 comments, last by Spippo 19 years, 2 months ago
Hi, I'm trying to make a game that can access my mysql-database on a remote server. I've looked for this on google and found some stuff about ODBC and mysql++, and I have been trying this for 2 days, but I can't get it to work. Does anybody know a good way to do this, and perhaps give a small example on how to do it. Also, i'm programming in Micro$oft Visual Studio .NET. When I use the API, class, ... you prefer, what should I add to my additional libraries? And it must work under Windows (9x, 2k and XP) Thanks
Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic
If your working purely with MS based operating systems ADO is not all that bad, lets you use a lot of different data sources using SQL including anything that can be used via ODBC (hence mySQL)

I've only dabbled in it myself but if you want to take a look here's a url to get your started.

ADO C++ tutorial
Advertisement
Actually, the easiest thing to do is to read up on the MySQL C APi which tells you exactly how to connect to a server and submit queries.

Specifically, I'm quoting from that chapter:

Quote:

Application programs should use this general outline for interacting with MySQL:

1. Initialize the MySQL library by calling mysql_library_init(). The library can be either the mysqlclient C client library or the mysqld embedded server library, depending on whether the application was linked with the -libmysqlclient or -libmysqld flag.

2. Initialize a connection handler by calling mysql_init() and connect to the server by calling mysql_real_connect().

3. Issue SQL statements and process their results. (The following discussion provides more information about how to do this.)

4. Close the connection to the MySQL server by calling mysql_close().

5. End use of the MySQL library by calling mysql_library_end().
enum Bool { True, False, FileNotFound };
Here is a code snippit from out database class:

bool psMysqlConnection::Initialize(const char *host, const char *database,                              const char *user, const char *pwd){    // Create a mydb    conn=mysql_init(NULL);    // Conn is the valid connection to be used for mydb. Have to store the mydb to get    // errors if this call fails.    MYSQL *conn_check = mysql_real_connect(conn,host,user,pwd,database,0,NULL,CLIENT_FOUND_ROWS);    return (conn == conn_check);}


You can see the full file here.

------------
Andrew
In that mysql C API, they require the mysql.h file for include.
Which I don't have.

Have downloaded the mysql server 4.1 and installed it on my PC. In that directory, there is a directory called Include, which has a file called mysql.h in it.
When I copy that file into the directory of my game, it gives the error that it also requires mysql_com.h, so I copied that file from the Include directory into my game directory, but then it gives me the following error:

in the mysql_com.h file:
Quote:

c:\Documents and Settings\Papa\Mijn documenten\Visual Studio Projects\ado\mysql_com.h(155): error C2146: syntax error : missing ';' before identifier 'fd'


It refers to the line:
  my_socket fd;					/* For Perl DBI/dbd */


Why is this?
Is it because I don't need to use the mysql.h file from the mysql Server?
Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic
I suggest you refer to the developer zone for MySQL, where you can find all the documentation on using the API. You can also download the latest executables and binaries for your platform there (make sure you get the full package!)

However, the documentation assumes that you already know how to use your development environment. For example, when adding new header dependencies, you're supposed to add the library the headers are found from as an additional include path -- you're not supposed to copy files around. Similarly for libraries. The appropriate include paths for Windows are c:\mysql5\include (for headers) and c:\mysql5\lib\{debug,opt} (for libraries).

Additionally, I suggest you attempt to build the included examples first, if you have problems building the library within your own application. They should work; if they don't, you've done something wrong in your build environment.

Additionally, on Windows, you typically always want to include <windows.h> before other include files, and you typically want to include <winsock2.h> before <windows.h> if you're going to be using sockets.
enum Bool { True, False, FileNotFound };
Advertisement
OK,
I found how what I needed to do to include the includes and the libs in my project. (About the same way how the include it for SDL).
Then I made this little program:

#include <windows.h>#include <iostream>#include <mysql.h>using namespace std;int main(){    MYSQL *Link;    system("Pause");    return 0;}


This compiled great, gave me no errors,...

Then I tried the next step:
#include <windows.h>#include <iostream>#include <mysql.h>using namespace std;int main(){    MYSQL *Link;    Link = mysql_init(NULL);    system("Pause");    return 0;}

And there it gave me the following error:
Quote:

mysql error LNK2019: unresolved external symbol _mysql_init@4 referenced in function _main


So it still doesn't know the mysql_init function (I also tried mysql_real_connect, but it gave me the same kind of error)

Any Idea why?
Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic
Assuming Microsoft Visual C++ 6.0:

Go to Project->Settings->Link Tab->Object/Library Modules: and add

mysqlclient.lib

to the end of the list.

Should resolve the problem.

-=[ Megahertz ]=-
-=[Megahertz]=-
Hi,

I finaly got it to work, but now a got a new problem.
I made a whole new program and tested it step by step.
First I only used the mysql_init(), which didn't gave me an error.
Then I tried the mysql_real_connect(), which also didn't gave an error.
After this, I jumped to the last step, and tried the mysql_close(), which also worked.
The next step I tried, was to send a query (before the mysql_close())(I used "INSERT INTO Categories (ID, Category, CategoryID, Isle) VALUES (NULL, 'Equipment', '103', '10')" just as an example from a website) and this one did gave me an error:

Quote:

MySQL Server has gone.


Any idea why that is?
(I'm using a remote database (from servage.net) maybe they block commands from clients? But then they also should reject my connect right?)
Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic
A.2.8. MySQL server has gone away

This topic is closed to new replies.

Advertisement