Using Visual C++ 6.0 to connect to MySQL
Posted by:
Derek Keeler ()
Date: October 16, 2005 03:31AM
Hi all.
Probably been tutorialized like crazy here and across the Internets, but since I couldn't find it anywhere (at least with the google-ing that I did tonight) I figured I'd post my experience doing this here.
First some system notes:
MySQL 4.0.22 running on Linux (Gentoo) server
'mysql Ver 14.7 Distrib 4.1.12a, for Win32 (ia32)' running on local machine (Windows XP SP2)
Visual C++ ver 6.0 (Service pack 6)
The program:
Start Visual C++
Create a new Win32 console project (File->New and select 'Win32 Console Application')
Select the option to create a 'A simple application' and hit Ok
Under Project->Settings change the following for your 'Settings for Win32 Debug'
Tab: C/C++
--------------
[ Category: Preprocessor ]
Additional include directories:
<path to your Mysql installation>\include
[ Category: Code Generation ]
Use run-time library:
Multithreaded
[ Category: Precompiled Headers ]
select 'Not using precompiled headers'
Tab: Link
----------
[ Category: Input ]
Object/library modules: (append the following)
wsock32.lib mysqlclient.lib libmysql.lib mysys.lib
Ignore libraries:
LIBCMTD.lib
Additional library path:
<path to your MySql installation>\lib\debug
Then hit Ok. Now its time for code.
In the source file that the 'main.cpp', replace what is there with the following.
==================== SOURCE FILE CONTENTS ====================
#include <stdio.h>
#define W32_LEAN_AND_MEAN
#include <winsock2.h>
#include "mysql.h"
// change these to suit your setup
#define TABLE_OF_INTEREST "some_table"
#define SERVER_NAME "mysql_server"
#define DB_USER "user"
#define DB_USERPASS "pa55w0rd"
#define DB_NAME "db_name"
// prototypes
void showTables(MYSQL*);
void showContents(MYSQL*,const char*);
int main(int argc, char* argv[])
{
MYSQL *hnd=NULL; // mysql connection handle
const char *sinf=NULL; // mysql server information
hnd = mysql_init(NULL);
if (NULL == mysql_real_connect(hnd,SERVER_NAME,DB_USER,DB_USERPASS,DB_NAME,0,NULL,0))
{
fprintf(stderr,"Problem encountered connecting to the %s database on %s.\n",DB_NAME,SERVER_NAME);
}
else
{
fprintf(stdout,"Connected to the %s database on %s as user '%s'.\n",DB_NAME,SERVER_NAME,DB_USER);
sinf = mysql_get_server_info(hnd);
if (sinf != NULL)
{
fprintf(stdout,"Got server information: '%s'\n",sinf);
showTables(hnd);
showContents(hnd,TABLE_OF_INTEREST);
}
else
{
fprintf(stderr,"Failed to retrieve the server information string.\n");
}
mysql_close(hnd);
}
return 0;
}
void showTables(MYSQL *handle)
{
MYSQL_RES *result=NULL; // result of asking the database for a listing of its tables
MYSQL_ROW row; // one row from the result set
result = mysql_list_tables(handle,NULL);
row = mysql_fetch_row(result);
fprintf(stdout,"Tables found:\n\n");
while (row)
{
fprintf(stdout,"\t%s\n",row[0]);
row = mysql_fetch_row(result);
}
mysql_free_result(result);
fprintf(stdout,"\nEnd of tables\n");
return;
}
void showContents
(
MYSQL *handle,
const char *tbl
)
{
MYSQL_RES *res=NULL; // result of querying for all rows in table
MYSQL_ROW row; // one row returned
char sql[1024], // sql statement used to get all rows
commastr[2]; // to put commas in the output
int i,numf=0; // number of fields returned from the query
sprintf(sql,"select * from %s",tbl);
fprintf(stdout,"Using sql statement: '%s' to extract all rows from the specified table.\n",sql);
if (!mysql_query(handle,sql))
{
res = mysql_use_result(handle);
if (res)
{
numf = mysql_num_fields(res);
row = mysql_fetch_row(res);
fprintf(stdout,"Rows returned:\n\n");
while (row)
{
commastr[0]=commastr[1]=(char)NULL;
for (i=0;i<numf;i++)
{
if (row == NULL)
{
fprintf(stdout,"%sNULL",commastr);
}
else
{
fprintf(stdout,"%s%s",commastr,row);
}
commastr[0]=',';
}
fprintf(stdout,"\n");
row = mysql_fetch_row(res);
}
fprintf(stdout,"\nEnd of rows\n");
mysql_free_result(res);
}
else
{
fprintf(stderr,"Failed to use the result acquired!\n");
}
}
else
{
fprintf(stderr,"Failed to execute query. Ensure table is valid!\n");
}
return;
}
==================== END OF SOURCE FILE ====================
Altogether, this is an exceedingly simple example, and has very little in terms of help for the inexperienced. Regardless, I hope this helps the next poor soul who can't seem to get it together with google.
The libraries to include, and the extra's to include from Windows itself were my biggest hurdle. (Oh yes, and the setting to 'Multithreaded' took me a bit to figure out too).
Good luck to everyone, and sorry I can't format this message a bit better. Just copy/paste the code above and tab until pretty.
--derek