C program to print the last insertion row id of the MySQL table in Linux

Here, we are going to write a C program to print the last insertion row id of the MySQL table in Linux.
By Nidhi Last updated : March 10, 2024

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 we will get the last insertion row id of the specified table in MySql in Linux and then print the last insertion row id on the console screen.

Program/Source Code

The source code to print the last insertion row id of the MySql table in Linux is given below. The given program is compiled and executed successfully on Ubuntu 20.04.

//C program to print the last insertion row id of 
//MySql table 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";
    int rowid = 0;

    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;
    }

    rowid = mysql_insert_id(conn);
    printf("The last inserted row id is: %d\n", rowid);

    mysql_close(conn);

    return 0;
}

Output

$ gcc rowid.c -o rowid  `mysql_config --cflags --libs`
$ sudo ./rowid 
The last inserted row id is: 3

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 we created an "employee" table and then insert records into the table. After that, we got the last insertion row id using the mysql_insert_id() function and then print the last row id on the console screen.

C MySQL Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.