Re: vs 2008 c++ access mysql database
Download 32 bit version.
#include "mysql.h"
#pragma comment (lib,"libmysql")
#pragma comment (lib,"mysqlclient")
Copy all .h including in subdirectories, to files where your project files.
Copy all .lib files as well.
Copy libmysql.dll to where is your .exe program after the built.
YOu need a hosting MySql database, arvixe.com is cheap, $60 for 12 month.
And create MySql database.
I am not sure about bin directory, dll files. I am working on that right now.
And these two functions, example, one checks another inserts.
Code:
bool IP_IN_MYSQL(string CurrentIP)
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;//hello
char *server = "hosting.com";
char *user = "username";
char *password = "password"; /* set me first */
char *database = "namedatabase1";
conn = mysql_init(NULL);
while ( !mysql_real_connect(conn, server,user, password, database, 0, NULL, 0) )
{ ::MessageBox(HWND_DESKTOP,"Cannot connect to MySql","Help",MB_OK);
::MessageBox(HWND_DESKTOP,"To Resolve, Restart in Safe Mode with Network!","Help",MB_OK);
::MessageBox(HWND_DESKTOP,"Press F8 on a Boot, Choose Safe Mode with Network.","Help",MB_OK);
}
int ip = mysql_query(conn,"SELECT * FROM IP_TABLE");
res = mysql_use_result(conn);
string s2;
if (ip==0)
while ((row = mysql_fetch_row(res)) != NULL)
{
s2 = row[0];
if (CurrentIP==s2)
{ /* close connection */
mysql_free_result(res);
mysql_close(conn);
return true;
}
} // while loop
mysql_free_result(res);
mysql_close(conn);
return false;
}
void INSERT_INTO_MYSQL(string NewIp)
{
MYSQL *conn;
char *server = "hosting.com";
char *user = "susername";
char *password = "password"; /* set me first */
char *database = "databasename";
conn = mysql_init(NULL);
while ( !mysql_real_connect(conn, server,user, password, database, 0, NULL, 0) )
{
::MessageBox(HWND_DESKTOP,"Cannot connect to MySql","Help",MB_OK);
::MessageBox(HWND_DESKTOP,"Press: CTRL+ALT+DEL to Restart.","Help",MB_OK);
}
string input = "INSERT INTO IP_TABLE(IP_ID) VALUES('";
input = input + NewIp + "')";
char *input_str = new char[input.size()+1];
strcpy(input_str, input.c_str());
int ip = mysql_query(conn,input_str);
delete[] input_str;
mysql_close(conn);
}
I hope it helps, just make your own table name.
Edited 1 time(s). Last edit at 01/22/2011 11:32PM by Surgey Pushkin.