Connect with MySQL using C in ubuntu
4 posters
IITDU Forum :: Academics (BIT) :: Semester 5 :: CSE 501
Page 1 of 1
Connect with MySQL using C in ubuntu
It is possible to connect with MySQL using C. Here I am directing a way for that.
First of all you need
Now we write a code to connect with mysql and which shows all the tables in database
To link the extra header files (like mysql.h) we will use some extra parameter to compile the code. Suppose the file is in desktop the name of the file is "input-file.c". So the compilation command will be
Now, it is ready to run.
First of all you need
- mysql server(sudo apt-get install mysql-server)
- A library for developers(libmysqlclient-dev)
Now we write a code to connect with mysql and which shows all the tables in database
- Code:
/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>
#include <stdio.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
//set the password for mysql server here
char *password = ""; /* set me first */
char *database = "mysql";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
To link the extra header files (like mysql.h) we will use some extra parameter to compile the code. Suppose the file is in desktop the name of the file is "input-file.c". So the compilation command will be
- Code:
cd Desktop/
gcc -o output-file $(mysql_config --cflags) input-file.c $(mysql_config --libs)
Now, it is ready to run.
- Code:
./output-file
Last edited by BIT0107-Toma on Sun Feb 27, 2011 2:04 pm; edited 1 time in total
BIT0107-Toma- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 280
Points : 453
Re: Connect with MySQL using C in ubuntu
this is a very very good job
BIT0115-Efat- Service Release
- Course(s) :
- BIT
Blood Group : O+
Posts : 779
Points : 1120
Similar topics
» Using Squirrel SQL client to Connect to Oracle 10G in ubuntu
» some commands for mysql.....
» Command for MySQL of DBMS-I
» Various commands for DBMS and MySQL
» MYSQL commands Collection
» some commands for mysql.....
» Command for MySQL of DBMS-I
» Various commands for DBMS and MySQL
» MYSQL commands Collection
IITDU Forum :: Academics (BIT) :: Semester 5 :: CSE 501
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford