Skip navigation links

MySQL Forums :: C/C++ :: Using Visual C++ 6.0 to connect to MySQL


Advanced Search

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

Options: ReplyQuote


Subject Written By Posted
Using Visual C++ 6.0 to connect to MySQL Derek Keeler 10/16/2005 03:31AM
Re: Using Visual C++ 6.0 to connect to MySQL jmoravec 01/27/2006 03:44AM
Re: Using Visual C++ 6.0 to connect to MySQL Joris Van den Dries 01/31/2006 04:29PM
Re: Using Visual C++ 6.0 to connect to MySQL Alan zhang 08/18/2006 08:52PM
Re: Using Visual C++ 6.0 to connect to MySQL Jimin Liu 09/26/2006 11:26PM
Re: Using Visual C++ 6.0 to connect to MySQL alan herr 02/02/2006 03:39AM
Re: Using Visual C++ 6.0 to connect to MySQL Anton Remeli 08/09/2006 03:42AM
Re: Using Visual C++ 6.0 to connect to MySQL Marcin Wasyl 10/26/2006 03:06PM
Re: Using Visual C++ 6.0 to connect to MySQL Arvind Arya 03/29/2007 11:33PM
Using Visual C++ 6.0 to connect to MySQL Arvind Arya 03/30/2007 11:37PM
Re: Using Visual C++ 6.0 to connect to MySQL lala Ma 06/21/2007 02:11AM
Re: Using Visual C++ 6.0 to connect to MySQL mike patton 06/26/2007 11:00PM
Re: Using Visual C++ 6.0 to connect to MySQL radha krishna 12/31/2007 11:16PM
Re: Using Visual C++ 6.0 to connect to MySQL ashraf dagher 01/28/2008 07:24PM
Re: Using Visual C++ 6.0 to connect to MySQL Jon Wolff 11/07/2008 10:16PM
Re: Using Visual C++ 6.0 to connect to MySQL Erix Kivuti 03/11/2008 07:17AM
Using Visual C++ 6.0 to connect to MySQL using DLL Sundar Shrestha 04/29/2008 12:34AM
Re: Using Visual C++ 6.0 to connect to MySQL using DLL Peter R 05/20/2008 06:22PM
Re: Using Visual C++ 6.0 to connect to MySQL Peter R 05/07/2008 03:20AM
Re: Using Visual C++ 6.0 to connect to MySQL Sam F 10/02/2008 06:17PM
Using Visual C++ 6.0 to connect to MySQL Srinivasulu m 11/27/2008 12:19AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.