/******************************************* 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: \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: \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. U slucaju da pravi problema iz Tab:Link Object/library modules: (append the following) izbaci mysqlclient.lib Ako postoji jos jedna main funkcija u drugom fajlu koji je napravio Visual C++ obrisi je In the source file that the 'main.cpp', replace what is there with the following. ==================== SOURCE FILE CONTENTS ==================== #include #define W32_LEAN_AND_MEAN #include #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 #define W32_LEAN_AND_MEAN #include #include // change these to suit your setup #define TABLE_OF_INTEREST "student" #define SERVER_NAME "localhost" #define DB_USER "srle" #define DB_USERPASS "" //u slucaju da nema password ostavis samo prazne "" #define DB_NAME "ucenje" // prototypes void showTables(MYSQL*); void showContents(MYSQL*,const char*); void showDatabases(MYSQL*); void showPravljenjeTabele(MYSQL*); 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); /***************************************************/ /* Get host, server and protocol info */ printf("Host: %s\n", mysql_get_host_info(hnd)); printf("Server: %s\n", mysql_get_server_info(hnd)); printf("Protocol: %d\n", mysql_get_proto_info(hnd)); /****************************************************/ if (sinf != NULL) { fprintf(stdout,"Got server information: '%s'\n",sinf); showTables(hnd); showContents(hnd,TABLE_OF_INTEREST); showDatabases(hnd); showPravljenjeTabele(hnd); /* Ja sam napravio databazu imena_gradova */ mysql_query(hnd, "CREATE DATABASE imena_gradova"); } 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