Connect with MySQL using C in ubuntu554

Connect with MySQL using C in ubuntu

Post new topic   Reply to topic

View previous topic View next topic Go down

Connect with MySQL using C in ubuntu

Post by BIT0107-Toma on Tue Feb 22, 2011 2:02 pm

It is possible to connect with MySQL using C. Here I am directing a way for that.

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 11:04 am; edited 1 time in total

_________________________________________________________________
Take it easy, make it easy

BIT0107-Toma
Programmer
Programmer

Course(s):
  • BIT

Blood Group: B+
Posts: 280
Points: 453

View user profile

Back to top Go down

Re: Connect with MySQL using C in ubuntu

Post by BIT0122-Amit on Tue Feb 22, 2011 7:43 pm

Good work Very Happy thanks!

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|

BIT0122-Amit
Founder
Founder

Course(s):
  • BIT

Blood Group: O+
Posts: 4187
Points: 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Connect with MySQL using C in ubuntu

Post by BIT0115-Efat on Sun Feb 27, 2011 2:29 pm

this is a very very good job rep++

BIT0115-Efat
Service Release
Service Release

Course(s):
  • BIT

Blood Group: O+
Posts: 779
Points: 1120

View user profile

Back to top Go down

Re: Connect with MySQL using C in ubuntu

Post by BIT0105-kanak on Sun Feb 27, 2011 3:08 pm

well done rep++

BIT0105-kanak
Alpha Release
Alpha Release

Posts: 46
Points: 68

View user profile http://mitaly.webs.com

Back to top Go down

View previous topic View next topic Back to top


Post new topic   Reply to topic
Permissions in this forum:
You cannot reply to topics in this forum