Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect and insert record to mysql using c language? [closed]

Hello friends,

My question is, can i connect mysql with c language? And if connection is possible so how to insert record to mysql database. Please give simple and small example.

If any query so comment please.

like image 932
Er CEO Vora Mayur Avatar asked Jul 01 '17 12:07

Er CEO Vora Mayur


1 Answers

You will need to use the MySQL C Connector which your can find on their website: https://dev.mysql.com/downloads/connector/c/

Concerning your request regarding the example, a simple research would have helped you: Mysql INSERT statement in C

Nothing beats the manual though: https://dev.mysql.com/doc/refman/5.7/en/c-api-function-overview.html

EDIT:

Here is a simple example:

sql.c:

#include <stdio.h>                                                                                   
#include <stdlib.h>                                                                                  
#include <mysql/mysql.h>                                                                             

int main(void)                                                                                       
{                                                                                                    
  MYSQL *conn;                                                                                       

  if ((conn = mysql_init(NULL)) == NULL)                                                             
  {                                                                                                  
    fprintf(stderr, "Could not init DB\n");                                                 
    return EXIT_FAILURE;                                                                             
  }                                                                                                  
  if (mysql_real_connect(conn, "localhost", "user", "passwd", "dbname", 0, NULL, 0) == NULL)             
  {                                                                                                  
    fprintf(stderr, "DB Connection Error\n");                                                        
    return EXIT_FAILURE;                                                                             
  }                                                                                                  
  if (mysql_query(conn, "INSERT INTO table_1 (test) VALUES ('Hello World')") != 0)                   
  {                                                                                                  
    fprintf(stderr, "Query Failure\n");                                                              
    return EXIT_FAILURE;                                                                             
  }                                                                                                  
  mysql_close(conn);                                                                                 
  return EXIT_SUCCESS;                                                                               
}

gcc sql.c -o sql -lmysqlclient

like image 141
Ra'Jiska Avatar answered Oct 23 '22 05:10

Ra'Jiska