Open and Read Sqlite Databasse Files Reddit

SQLite - C/C++


In this affiliate, you will learn how to utilize SQLite in C/C++ programs.

Installation

Before you beginning using SQLite in our C/C++ programs, you lot need to make sure that you have SQLite library prepare on the machine. You lot tin can cheque SQLite Installation chapter to understand the installation procedure.

C/C++ Interface APIs

Following are of import C/C++ SQLite interface routines, which tin can suffice your requirement to piece of work with SQLite database from your C/C++ program. If you lot are looking for a more sophisticated application, then you can expect into SQLite official documentation.

Sr.No. API & Description
1

sqlite3_open(const char *filename, sqlite3 **ppDb)

This routine opens a connection to an SQLite database file and returns a database connexion object to be used past other SQLite routines.

If the filename argument is Zippo or ':memory:', sqlite3_open() will create an in-retentivity database in RAM that lasts just for the elapsing of the session.

If the filename is not Zippo, sqlite3_open() attempts to open up the database file by using its value. If no file by that proper noun exists, sqlite3_open() volition open a new database file past that proper noun.

2

sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void *data, char **errmsg)

This routine provides a quick, like shooting fish in a barrel way to execute SQL commands provided by sql argument which tin consist of more i SQL control.

Here, the first argument sqlite3 is an open database object, sqlite_callback is a call dorsum for which data is the 1st argument and errmsg will be returned to capture any error raised by the routine.

SQLite3_exec() routine parses and executes every command given in the sql argument until it reaches the finish of the string or encounters an mistake.

iii

sqlite3_close(sqlite3*)

This routine closes a database connexion previously opened by a call to sqlite3_open(). All prepared statements associated with the connection should exist finalized prior to closing the connection.

If any queries remain that have not been finalized, sqlite3_close() will render SQLITE_BUSY with the mistake bulletin Unable to close due to unfinalized statements.

Connect To Database

Following C code segment shows how to connect to an existing database. If the database does not be, and then it will be created and finally a database object volition exist returned.

#include <stdio.h> #include <sqlite3.h>   int master(int argc, char* argv[]) {    sqlite3 *db;    char *zErrMsg = 0;    int rc;     rc = sqlite3_open("examination.db", &db);     if( rc ) {       fprintf(stderr, "Can't open database: %s\north", sqlite3_errmsg(db));       render(0);    } else {       fprintf(stderr, "Opened database successfully\n");    }    sqlite3_close(db); }        

Now, let's compile and run the above program to create our database test.db in the current directory. You can change your path as per your requirement.

$gcc test.c -50 sqlite3 $./a.out Opened database successfully        

If y'all are going to use C++ source lawmaking, then you can compile your code every bit follows −

$g++ test.c -l sqlite3        

Hither, nosotros are linking our program with sqlite3 library to provide required functions to C plan. This volition create a database file test.db in your directory and you lot will have the following outcome.

-rwxr-xr-ten. i root root 7383 May viii 02:06 a.out -rw-r--r--. 1 root root  323 May 8 02:05 test.c -rw-r--r--. i root root    0 May 8 02:06 test.db        

Create a Table

Post-obit C lawmaking segment will exist used to create a table in the previously created database −

#include <stdio.h> #include <stdlib.h> #include <sqlite3.h>   static int callback(void *NotUsed, int argc, char **argv, char **azColName) {    int i;    for(i = 0; i<argc; i++) {       printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");    }    printf("\n");    return 0; }  int main(int argc, char* argv[]) {    sqlite3 *db;    char *zErrMsg = 0;    int rc;    char *sql;     /* Open database */    rc = sqlite3_open("test.db", &db);        if( rc ) {       fprintf(stderr, "Can't open database: %s\north", sqlite3_errmsg(db));       return(0);    } else {       fprintf(stdout, "Opened database successfully\n");    }     /* Create SQL statement */    sql = "CREATE Table Visitor("  \       "ID INT Principal Central     Non Cipher," \       "NAME           TEXT    Non Nix," \       "Age            INT     Not Null," \       "Accost        CHAR(50)," \       "Salary         Real );";     /* Execute SQL argument */    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);        if( rc != SQLITE_OK ){       fprintf(stderr, "SQL error: %s\north", zErrMsg);       sqlite3_free(zErrMsg);    } else {       fprintf(stdout, "Table created successfully\north");    }    sqlite3_close(db);    render 0; }        

When the to a higher place programme is compiled and executed, it will create Visitor tabular array in your examination.db and the final listing of the file will exist as follows −

-rwxr-xr-ten. 1 root root 9567 May viii 02:31 a.out -rw-r--r--. 1 root root 1207 May 8 02:31 exam.c -rw-r--r--. 1 root root 3072 May eight 02:31 exam.db        

INSERT Operation

Post-obit C code segment shows how you can create records in Visitor table created in the in a higher place instance −

#include <stdio.h> #include <stdlib.h> #include <sqlite3.h>   static int callback(void *NotUsed, int argc, char **argv, char **azColName) {    int i;    for(i = 0; i<argc; i++) {       printf("%southward = %s\n", azColName[i], argv[i] ? argv[i] : "Nada");    }    printf("\n");    return 0; }  int main(int argc, char* argv[]) {    sqlite3 *db;    char *zErrMsg = 0;    int rc;    char *sql;     /* Open database */    rc = sqlite3_open("test.db", &db);        if( rc ) {       fprintf(stderr, "Tin can't open database: %s\n", sqlite3_errmsg(db));       render(0);    } else {       fprintf(stderr, "Opened database successfully\n");    }     /* Create SQL statement */    sql = "INSERT INTO COMPANY (ID,Proper noun,AGE,ADDRESS,SALARY) "  \          "VALUES (1, 'Paul', 32, 'California', 20000.00 ); " \          "INSERT INTO Visitor (ID,NAME,Historic period,ADDRESS,Salary) "  \          "VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); "     \          "INSERT INTO Visitor (ID,Proper name,AGE,Accost,Bacon)" \          "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );" \          "INSERT INTO Company (ID,Name,Historic period,Accost,SALARY)" \          "VALUES (four, 'Mark', 25, 'Rich-Mond ', 65000.00 );";     /* Execute SQL statement */    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);        if( rc != SQLITE_OK ){       fprintf(stderr, "SQL error: %due south\n", zErrMsg);       sqlite3_free(zErrMsg);    } else {       fprintf(stdout, "Records created successfully\n");    }    sqlite3_close(db);    return 0; }        

When the above program is compiled and executed, it will create the given records in COMPANY table and will display the following two lines −

Opened database successfully Records created successfully        

SELECT Operation

Before proceeding with actual example to fetch records, let us look at some detail near the callback part, which we are using in our examples. This callback provides a way to obtain results from SELECT statements. It has the following declaration −

typedef int (*sqlite3_callback)(    void*,    /* Data provided in the 4th argument of sqlite3_exec() */    int,      /* The number of columns in row */    char**,   /* An array of strings representing fields in the row */    char**    /* An array of strings representing column names */ );        

If the above callback is provided in sqlite_exec() routine as the third argument, SQLite will call this callback function for each record processed in each SELECT argument executed inside the SQL argument.

Post-obit C lawmaking segment shows how you can fetch and brandish records from the Visitor table created in the in a higher place instance −

#include <stdio.h> #include <stdlib.h> #include <sqlite3.h>   static int callback(void *data, int argc, char **argv, char **azColName){    int i;    fprintf(stderr, "%s: ", (const char*)information);        for(i = 0; i<argc; i++){       printf("%southward = %south\n", azColName[i], argv[i] ? argv[i] : "NULL");    }        printf("\due north");    return 0; }  int master(int argc, char* argv[]) {    sqlite3 *db;    char *zErrMsg = 0;    int rc;    char *sql;    const char* data = "Callback part called";     /* Open database */    rc = sqlite3_open("examination.db", &db);        if( rc ) {       fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));       return(0);    } else {       fprintf(stderr, "Opened database successfully\n");    }     /* Create SQL statement */    sql = "SELECT * from COMPANY";     /* Execute SQL statement */    rc = sqlite3_exec(db, sql, callback, (void*)information, &zErrMsg);        if( rc != SQLITE_OK ) {       fprintf(stderr, "SQL error: %s\northward", zErrMsg);       sqlite3_free(zErrMsg);    } else {       fprintf(stdout, "Performance washed successfully\n");    }    sqlite3_close(db);    return 0; }        

When the above program is compiled and executed, it volition produce the following issue.

Opened database successfully Callback part chosen: ID = 1 NAME = Paul AGE = 32 Accost = California Salary = 20000.0  Callback function chosen: ID = two NAME = Allen Age = 25 ADDRESS = Texas Bacon = 15000.0  Callback function called: ID = iii Proper noun = Teddy Age = 23 ADDRESS = Norway SALARY = 20000.0  Callback function called: ID = 4 NAME = Mark AGE = 25 Address = Rich-Mond SALARY = 65000.0  Operation done successfully        

UPDATE Operation

Post-obit C code segment shows how we can utilize UPDATE statement to update any record and and then fetch and display updated records from the Visitor tabular array.

#include <stdio.h> #include <stdlib.h> #include <sqlite3.h>   static int callback(void *data, int argc, char **argv, char **azColName){    int i;    fprintf(stderr, "%southward: ", (const char*)data);        for(i = 0; i<argc; i++) {       printf("%s = %s\northward", azColName[i], argv[i] ? argv[i] : "Cypher");    }    printf("\n");    return 0; }  int main(int argc, char* argv[]) {    sqlite3 *db;    char *zErrMsg = 0;    int rc;    char *sql;    const char* data = "Callback role called";     /* Open up database */    rc = sqlite3_open("test.db", &db);        if( rc ) {       fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));       return(0);    } else {       fprintf(stderr, "Opened database successfully\n");    }     /* Create merged SQL statement */    sql = "UPDATE Company set SALARY = 25000.00 where ID=one; " \          "SELECT * from Company";     /* Execute SQL argument */    rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);        if( rc != SQLITE_OK ) {       fprintf(stderr, "SQL error: %southward\n", zErrMsg);       sqlite3_free(zErrMsg);    } else {       fprintf(stdout, "Functioning done successfully\northward");    }    sqlite3_close(db);    render 0; }        

When the above plan is compiled and executed, information technology will produce the post-obit consequence.

Opened database successfully Callback function chosen: ID = 1 Proper noun = Paul AGE = 32 ADDRESS = California SALARY = 25000.0  Callback part called: ID = 2 NAME = Allen AGE = 25 ADDRESS = Texas Bacon = 15000.0  Callback function chosen: ID = 3 NAME = Teddy Historic period = 23 Accost = Kingdom of norway Salary = 20000.0  Callback role called: ID = 4 NAME = Mark Age = 25 ADDRESS = Rich-Mond SALARY = 65000.0  Operation washed successfully        

DELETE Operation

Following C lawmaking segment shows how yous can use DELETE statement to delete whatever record and then fetch and display the remaining records from the Visitor table.

#include <stdio.h> #include <stdlib.h> #include <sqlite3.h>   static int callback(void *data, int argc, char **argv, char **azColName) {    int i;    fprintf(stderr, "%s: ", (const char*)data);        for(i = 0; i<argc; i++) {       printf("%s = %s\due north", azColName[i], argv[i] ? argv[i] : "NULL");    }    printf("\n");    return 0; }  int principal(int argc, char* argv[]) {    sqlite3 *db;    char *zErrMsg = 0;    int rc;    char *sql;    const char* data = "Callback function called";     /* Open database */    rc = sqlite3_open("test.db", &db);        if( rc ) {       fprintf(stderr, "Can't open database: %southward\n", sqlite3_errmsg(db));       render(0);    } else {       fprintf(stderr, "Opened database successfully\n");    }     /* Create merged SQL statement */    sql = "DELETE from Visitor where ID=2; " \          "SELECT * from COMPANY";     /* Execute SQL statement */    rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);        if( rc != SQLITE_OK ) {       fprintf(stderr, "SQL error: %south\n", zErrMsg);       sqlite3_free(zErrMsg);    } else {       fprintf(stdout, "Operation done successfully\north");    }    sqlite3_close(db);    return 0; }        

When the above program is compiled and executed, it will produce the following event.

Opened database successfully Callback function called: ID = i Proper noun = Paul AGE = 32 Address = California SALARY = 20000.0  Callback function chosen: ID = 3 NAME = Teddy Historic period = 23 Accost = Kingdom of norway Bacon = 20000.0  Callback function called: ID = 4 NAME = Mark AGE = 25 ADDRESS = Rich-Mond SALARY = 65000.0  Operation done successfully        

Useful Video Courses


Android SQLite Programming for Beginners

Video

Local SQLite Database with Node for beginners

Video

Angular 12, Python Django & SQLite Full Stack Web Development

Video

hazelwoodexambeir1955.blogspot.com

Source: https://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm

0 Response to "Open and Read Sqlite Databasse Files Reddit"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel