C program to create SQLite database dynamically in Linux

Here, we are going to write a C program to create SQLite database dynamically in Linux.
By Nidhi Last updated : March 10, 2024

Prerequisites

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

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

Problem Solution

In this program, we will create a database using the sqlite3_open() function dynamically in Linux.

Program/Source Code

The source code to create a SQLite database dynamically is given below. The given program is compiled and executed successfully on Ubuntu 20.04.

//C program to create database dynamically in linux.

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

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

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

    if (ret == SQLITE_OK) {
        printf("Database created successfully\n");
    }

    sqlite3_close(db_ptr);

    return 0;
}

Output

$ gcc create_db.c -o create_db -lsqlite3 -std=c99
$ ./create_db 
Database created successfully

Explanation

In the above program, we included the sqlite3.h header file to uses SQLITE related functions. Here, we created "MyDb.db" database using sqlite_open() function. The sqlite_open() function is used to open an existing database and if the database is not present then it will create a specified database automatically.

C SQLite Programs »


Comments and Discussions!

Load comments ↻






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