Q:

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

belongs to collection: C MySQL Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 serverusernamepassword, 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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to get the column names of the MySQL dat... >>
<< C program to get records from the MySQL database t...