[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
|
[news.eclipse.dtp] Re: Connect to MySql 5.0
|
- From: sabeesh.cs@xxxxxxxxx (Sabeesh C.S)
- Date: Mon, 26 Feb 2007 05:41:24 +0000 (UTC)
- Newsgroups: eclipse.dtp
- Organization: Eclipse
- User-agent: NewsPortal/0.36 (http://florian-amrhein.de/newsportal)
Hi,
I am using Linux Fedora core4, Eclipse 3.2. and using CDT for developing
my programe and MYSQL 4.1.11 as database. I have used to establish a
connection between CDT ( C / C++) and MYSQL, I have used the following
command, and it is working. Make this programe giving this option in
right click on the project -> Properties -> C/C++ Build-> GCC C++ Linker
-> Libraries -> Library Search path ( -L ) and add the option
"/usr/X11R6/lib -lX11 -L/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
" without double quorts.
create a header file named " ConnectCMYSQL.h "
#include <mysql.h>
/*This function is used for connecting the data base */
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int C_Connect_MYSQL( char *hostname, char *username, char *passwd, char
*dbname )
{
mysql_init(&mysql);
if(!mysql_real_connect(&mysql, hostname, username, passwd, dbname,
0, NULL, 0))
{
printf("%d: %s \n", mysql_errno(&mysql),
mysql_error(&mysql));
printf("Connection Refused by MYSQL\n");
return 1;
}
else
{
printf("Connect\n");
return 0;
}
void Exectue_Query (char *Exec_Qry)
{
if(mysql_query(&mysql, Exec_Qry))
{
fprintf(stderr, "%d: %s\n", mysql_errno(&mysql),
mysql_error(&mysql));
}
else
{
result = mysql_store_result(&mysql);
printf("Qry Execute < %s > \n", Exec_Qry);
while(row = mysql_fetch_row(result))
{
printf("%s, %s - %s \n", row[1], row[2], row[3]);
}
mysql_free_result(result);
mysql_close(&mysql);
}
}
}
Create a file main.c /main.cpp
---------------------------------
#include <X11/Xlib.h>
#include <stdio.h>
#include <ConnectCMYSQL.h> /*For MYSQL Connection */
int main(int argc, char* argv[])
{
char *Qry;
C_Connect_MYSQL("localhost","username", "password", "Database name");
/*Try to create a connection with database */
Qry = "select * from TABLENAME ;" ;
Exectue_Query(Qry);
}