ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C program to insert data into a table in MySQL database in Linux

Here, we are going to write a C program to insert data into a table in MySQL database in Linux.
Submitted by Nidhi, on February 13, 2021

Prerequisites:

The following packages need to be installed in the ubuntu machine for MySQL connectivity using the C program.

sudo apt install default-libmysqlclient-dev

Problem Solution:

In this program, we will connect to the MyDb database and then insert data into the "Employee" table using the "INSERT INTO" command.

Program/Source Code:

The source code to insert data into a specified table in the MySQL database in Linux is given below. The given program is compiled and executed successfully on Ubuntu 20.04.

//C program to insert data  in the table in 
//MySQL database dynamically in Linux.

#include <mysql.h>
#include <stdio.h>
#include <string.h>

int main()
{
    char server[16] = "localhost";
    char username[16] = "root";
    char password[16] = "root";
    char database[16] = "MyDb";

    MYSQL* conn = mysql_init(NULL);

    if (conn == NULL) {
        printf("MySQL initialization failed");
        return 1;
    }

    if (mysql_real_connect(conn, server, username, password, database, 0, NULL, 0) == NULL) {
        printf("Unable to connect with MySQL server\n");
        mysql_close(conn);
        return 1;
    }

    if (mysql_query(conn, "CREATE TABLE Employee(Eid INT, Ename VARCHAR(16), Salary INT)")) {
        printf("Unable to create database table in MyDb database\n");
        mysql_close(conn);
        return 1;
    }

    if (mysql_query(conn, "insert into Employee values(101,'KIRAN', 10000)")) {
        printf("Unable to insert data into Employee table\n");
        mysql_close(conn);
        return 1;
    }
    if (mysql_query(conn, "Insert into Employee values(102,'SUMAN', 12000)")) {
        printf("Unable to insert data into Employee table\n");
        mysql_close(conn);
        return 1;
    }

    if (mysql_query(conn, "Insert into Employee values(103,'RAMAN', 16000)")) {
        printf("Unable to insert data into Employee table\n");
        mysql_close(conn);
        return 1;
    }

    mysql_close(conn);

    printf("Data inserted successfully\n");

    return 0;
}

Output:

$ gcc insert.c -o insert  `mysql_config --cflags --libs`
[email protected]:~/MySql_Program$ sudo ./insert 
Data inserted successfully

In the above program, we included the mysql.h header file to use MySql connectivity related functions. Here, we created variables server, username, password, and database that are initialized values specific to the MySQL connectivity.

Here, we connected to the MyDb database in the MySql server using the mysql_real_connect() function and then create an "employee" table and insert records into the employee table using mysql_query() function with specified SQL command and then print "Data inserted successfully" message on the console screen.

C MySQL Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.