Q:

C program to delete records from a database table in SQLite

belongs to collection: C SQLite Programs

0

C program to delete records from a database table in SQLite

All Answers

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

In this program, we will delete records from the employee table using source code in SQLite.

Program/Source Code:

The source code to delete records from a database table in Linux is given below. The given program is compiled and executed successfully on Ubuntu 20.04.

//C program to delete records from a database table in SQLite.

#include <sqlite3.h>
#include <stdio.h>

int main(void)
{
    sqlite3* db_ptr;
    char* errMesg = 0;

    int ret = 0;

    ret = sqlite3_open("MyDb.db", &db_ptr);

    if (ret != SQLITE_OK) {
        printf("Database opening error\n");
    }

    char* sql_stmt = "DELETE FROM Employee";

    ret = sqlite3_exec(db_ptr, sql_stmt, 0, 0, &errMesg);

    if (ret != SQLITE_OK) {

        printf("Error in SQL statement: %s\n", errMesg);

        sqlite3_free(errMesg);
        sqlite3_close(db_ptr);

        return 1;
    }

    printf("Employee records deleted successfully\n");
    sqlite3_close(db_ptr);

    return 0;
}

Output:

$ gcc delete.c -o delete -lsqlite3 -std=c99
$ ./delete 
Employee records deleted successfully
$ sqlite3 MyDb.db 
SQLite version 3.31.1 2020-01-27 19:55:54
Enter ".help" for usage hints.
sqlite> select * from employee;

In the above program, we included the sqlite3.h header file to uses SQLite related functions. Here, we deleted records of employee table in "MyDb.db" database using sqlite3_exec() by passing specified SQL statement.

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

total answers (1)

C program to get records from a database table in ... >>
<< C program to insert data into a database table in ...