IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Connect with MySQL using C in ubuntu

4 posters

Go down

Connect with MySQL using C in ubuntu Empty Connect with MySQL using C in ubuntu

Post by BIT0107-Toma Tue Feb 22, 2011 5: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 2:04 pm; edited 1 time in total
BIT0107-Toma
BIT0107-Toma
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 280
Points : 453

Back to top Go down

Connect with MySQL using C in ubuntu Empty Re: Connect with MySQL using C in ubuntu

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

Good work Very Happy thanks!
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Connect with MySQL using C in ubuntu Empty Re: Connect with MySQL using C in ubuntu

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

this is a very very good job rep++
BIT0115-Efat
BIT0115-Efat
Service Release
Service Release

Course(s) :
  • BIT

Blood Group : O+
Posts : 779
Points : 1120

Back to top Go down

Connect with MySQL using C in ubuntu Empty Re: Connect with MySQL using C in ubuntu

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

well done rep++
BIT0105-kanak
BIT0105-kanak
Alpha Release
Alpha Release

Posts : 46
Points : 68

http://mitaly.webs.com

Back to top Go down

Connect with MySQL using C in ubuntu Empty Re: Connect with MySQL using C in ubuntu

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum