C with MySql Data Base on Linux

//////////////CONNECT C CODE WITH MYSQL/////////////////
->First we have to include MYSQL.h header file.Here we are using 4 pointers name as server, user, passward ,database and following are the compile and run command on LINUX O.S
//////////With gcc compiler On LINUX O.S////////////
->gcc -o filename $(mysql_config --cflags) filename.c $(mysql_config --libs)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "/usr/include/mysql/mysql.h"

main()
{
 MYSQL *conn;
 MYSQL_RES *res = NULL;
 MYSQL_ROW row;

 char *server="localhost";           /*server name or ip on which you want to connect c code with mysql database*/
 char *user = "root";                       /*user of that system*/
 char *password ="XYZ";             /*password of user of that system*/
 char *database = "sharjeel";     /*database name of that system on which you are connecting c code with mysql*/
    char query[100]="Select * from student_05";
 conn = mysql_init(NULL);

 if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0)){
      printf("Problem in Connecting To Database.......%s\n",database);
      if(conn)
           mysql_close(conn);
      printf("Connection closed");
 }else{
      printf("\nDatabase Connected. Now Looking For Emp Table\n");
       if(!mysql_query(conn,query)){
       printf("\nTable Exist in Database\n");
           if(conn) 
                 mysql_close(conn);
          }else{
                   printf("\nTable Doesn't Exist\n");
                   strcpy(query,"create table emp(name varchar(20))");
                   if(!mysql_query(conn,query)){
                        printf("\nTable Created\n");
                        if(conn)
                             mysql_close(conn);
                    }else{
                        printf("\nError in Executing Query\n");
                        if(conn)
                             mysql_close(conn);
                   }
          }
 }
mysql_free_result(res);
 mysql_close(conn);
}
 ###COMMANDS TO COPILE THIS CODE###
//////////With gcc compiler On Windows O.S////////////
COMPILE COMMAND::::
gcc "Path with  FileName.c"  -o "Path with File Name.exe" -L"Path of MySql connector with linking library"  -I"Path of MySql connector with include  library " -llibmysql
For Example:
->gcc c:\\mysql_connectivity\\experiment.c -o c:\\mysql_connectivity\\experiment.exe -L "C:\Program Files\MySQL\MySQL Connector C 6.0.2\lib\opt" -I "C:\Program Files\MySQL\MySQL Connector C 6.0.2\include" -llibmysql
RUN COMMAND::::::::
gcc "Path with FileName.c"
For Example:
->gcc c:\\mysql_connectivity\\experiment.c -o c:\\mysql_connectivity\\experiment.exe -L
 
/////////Cmpile simple c code on gcc compiler////////
Compile c program on gcc compiler
-> gcc -o filename filename.c
Here we have to write file name with path.

Run program on gcc compiler
-> ./filename

Comments

Popular posts from this blog

Error : DependencyManagement.dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: com.adobe.aem:uber-jar:jar:apis -> version 6.3.0 vs 6.4.0

Operators in Asterisk with Linux

ERROR Exception while handling event Sitecore.Eventing.Remote.PublishEndRemoteEventException: System.AggregateExceptionMessage: One or more exceptions occurred while processing the subscribers to the 'publish:end:remote'