Loading, please wait...

A to Z Full Forms and Acronyms

How to get record from a database table using parameterized SELECT query in SQLite in C programming | C programming

Mar 29, 2022 C programming, 2376 Views
How to get record from a database table using parameterized SELECT query in SQLite in C programming | C programming

How to get record from a database table using parameterized SELECT query in SQLite in C programming | C programming

We will get records from the employee table using source code and print all records on the console screen.

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

int main(void)
{
    sqlite3* db_ptr;
    sqlite3_stmt* stmt;
    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[64];

    strcpy(sql_stmt, "SELECT * FROM Employee where eid=");
    strcat(sql_stmt, "201");

    ret = sqlite3_prepare_v2(db_ptr, sql_stmt, -1, &stmt, 0);

    if (ret != SQLITE_OK) {
        printf("\nUnable to fetch data");
        sqlite3_close(db_ptr);
        return 1;
    }

    printf("Employee records\n");
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        printf("%s %s %s\n", sqlite3_column_text(stmt, 0), sqlite3_column_text(stmt, 1), sqlite3_column_text(stmt, 2));
    }

    sqlite3_finalize(stmt);
    sqlite3_close(db_ptr);

    return 0;
}
A to Z Full Forms and Acronyms