How to connect and insert a record in mysql using c language?

Hello friends,

My question is: can I connect mysql with c language? And if the connection is possible then how to insert the record into the mysql database. Please give a simple and small example.

If any request comments so, please.

+3


source to share


1 answer


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

Regarding your query for an example, a simple research would help you: Mysql INSERT statement in C

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

EDIT:



Here's 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

+3


source







All Articles