Evo moj kod za povezivanje sa mysql. Pisan je u c i koristi mysql.h koji ide uz instalaciju
sa MySql-om
Ja sam malo modifikovao derekov fajl koji sam skinuo sa interneta ali sam tu stavio i njegov i moj.
Code:
/*******************************************
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.
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 <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[i] == NULL)
{
fprintf(stdout,"%sNULL",commastr);
}
else
{
fprintf(stdout,"%s%s",commastr,row[i]);
}
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
******************************************************************
*/
/****Ovo ispod je modifikovan derekov fajl i preveden**********/
#include <stdio.h>
#define W32_LEAN_AND_MEAN
#include <winsock2.h>
#include <mysql.h> /*Ovo je postavljeno sa "< >" jer sam ja ukljucio mysql.h kao deo da moze da bude pretrazen iz prevodioca inace je tu putanje do foldera gde je instaliran
mysql i gde se i nalazi mysql.h ***************
*/
// change these to suit your setup
#define TABLE_OF_INTEREST "student"
#define SERVER_NAME "localhost"
#define DB_USER "root"
#define DB_USERPASS "srle" //ako nemas pasvord ostavi samo ""
#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<numf;i++)
{
if (row[i] == NULL)
{
fprintf(stdout,"%sNULL",commastr);
}
else
{
fprintf(stdout,"%s%s",commastr,row[i]);
}
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;
}
/***********************************************/
void showDatabases(MYSQL *handle)
{
MYSQL_RES *resource; /*result of querying for all rows in table*/
MYSQL_ROW result; /* one row returned */
/* Execute our SHOW DATABASES statement */
mysql_query(handle, "SHOW DATABASES");
/* Resource structure with the rows of data from SHOW DATABASES */
resource = mysql_use_result(handle);
printf("Databases:\n\n");
/* Fetch & print each row */
while((result = mysql_fetch_row(resource)))
{
printf("%s\n", result[0]);
}
/************************************/
/* prikazivanje tabela*/
mysql_query(handle, "show tables");
/*with rows of returned data. */
resource = mysql_use_result(handle);
printf("\nTabele u bazi ucenje:\n\n");
/* Fetch & print each row */
while((result = mysql_fetch_row(resource)))
{
printf("%s\n", result[0]);
}
return ;
}
/**********************************************************/
void showPravljenjeTabele(MYSQL *ruk)
{
//napravi tabelu test_table
#define DROP_SAMPLE_TABLE "DROP TABLE IF EXISTS test_table"
#define CREATE_SAMPLE_TABLE "CREATE TABLE test_table(col1 INT,\
col2 VARCHAR(40),\
col3 SMALLINT,\
col4 TIMESTAMP)"
MYSQL_RES *resource; /*result of querying for all rows in table*/
MYSQL_ROW result; /* one row returned */
/* Execute our SHOW DATABASES statement */
mysql_query(ruk, "show databases");
/* Resource structure with the rows of data from SHOW DATABASES */
resource = mysql_use_result(ruk);
printf("Databases:\n\n");
/* Fetch & print each row */
while((result = mysql_fetch_row(resource)))
{
printf("%s\n", result[0]);
}
/************************************/
/************************************/
/* prikazivanje tabela*/
mysql_query(ruk, "show tables");
/*with rows of returned data. */
resource = mysql_use_result(ruk);
printf("\nTabele u bazi ucenje:\n\n");
/* Fetch & print each row */
while((result = mysql_fetch_row(resource)))
{
printf("%s\n", result[0]);
}
/****************************************************/
/****pravljenje nove tabele*********/
if (mysql_query(ruk, DROP_SAMPLE_TABLE))
{
fprintf(stderr, " DROP TABLE failed\n");
fprintf(stderr, " %s\n", mysql_error(ruk));
exit(0);
}
if (mysql_query(ruk, CREATE_SAMPLE_TABLE))
{
fprintf(stderr, " CREATE TABLE failed\n");
fprintf(stderr, " %s\n", mysql_error(ruk));
exit(0);
}
/***********************************/
/*******Prikazivanje sadrzaja tabele************/
/*
void showContents ( MYSQL *handle, const char *tbl )
*/
mysql_query(ruk, CREATE_SAMPLE_TABLE);
#define TABELA_KOJA_MI_TREBA "ocene"
/*Prikazivanje sadrzaja tabele pozivanjem funkcije*/
showContents (ruk, TABELA_KOJA_MI_TREBA );
/************************************/
/* prikazivanje tabela*/
mysql_query(ruk, "describe student");
/*with rows of returned data. */
resource = mysql_use_result(ruk);
printf("\n Kolone u tabeli ocene:\n\n");
/* Fetch & print each row */
while((result = mysql_fetch_row(resource)))
{
printf("%s\n", result[0]);
}
/**************************************************/
return;
}
[Ovu poruku je menjao srecko.srecko dana 22.03.2009. u 13:54 GMT+1]
[Ovu poruku je menjao X Files dana 22.03.2009. u 14:10 GMT+1]