Loading, please wait...

A to Z Full Forms and Acronyms

C program to create database table dynamically in SQLite | C program

Mar 15, 2022 C program, 2581 Views
C program to create database table dynamically in SQLite | C program

C program to create database table dynamically in SQLite | C program

Prerequisites:

sudo apt install sqlite3
sudo apt install gcc
sudo apt install libsqlite3-dev

In this program, we will create a database table using source code dynamically 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");
    }

    ret = sqlite3_exec(db_ptr, "create table Employee (Eid INT, EName TEXT, Salary INT)", 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 table created successfully\n");
    sqlite3_close(db_ptr);

    return 0;
}
A to Z Full Forms and Acronyms