- SQLite Tutorial
- Advanced SQLite
- SQLite Interfaces
The first thing that comes to my mind is to do a bunch of t's, but that would cause words to be misaligned if any word is longer than any other word by a few characters. For example, I would like. A Hash Table in C/C (Associative array) is a data structure that maps keys to values.This uses a hash function to compute indexes for a key. Based on the Hash Table index, we can store the value at the appropriate location.
- SQLite Useful Resources
- Selected Reading
In this chapter, you will learn how to use SQLite in C/C++ programs.
Installation
Before you start using SQLite in our C/C++ programs, you need to make sure that you have SQLite library set up on the machine. You can check SQLite Installation chapter to understand the installation process.
C/C++ Interface APIs
Following are important C/C++ SQLite interface routines, which can suffice your requirement to work with SQLite database from your C/C++ program. If you are looking for a more sophisticated application, then you can look 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 connection object to be used by other SQLite routines. If the filename argument is NULL or ':memory:', sqlite3_open() will create an in-memory database in RAM that lasts only for the duration of the session. If the filename is not NULL, sqlite3_open() attempts to open the database file by using its value. If no file by that name exists, sqlite3_open() will open a new database file by that name. |
2 | sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void *data, char **errmsg) This routine provides a quick, easy way to execute SQL commands provided by sql argument which can consist of more than one SQL command. Here, the first argument sqlite3 is an open database object, sqlite_callback is a call back 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 end of the string or encounters an error. |
3 | sqlite3_close(sqlite3*) This routine closes a database connection previously opened by a call to sqlite3_open(). All prepared statements associated with the connection should be finalized prior to closing the connection. If any queries remain that have not been finalized, sqlite3_close() will return SQLITE_BUSY with the error message 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 exist, then it will be created and finally a database object will be returned.
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.
If you are going to use C++ source code, then you can compile your code as follows −
Here, we are linking our program with sqlite3 library to provide required functions to C program. This will create a database file test.db in your directory and you will have the following result.
Create a Table
Following C code segment will be used to create a table in the previously created database −
When the above program is compiled and executed, it will create COMPANY table in your test.db and the final listing of the file will be as follows −
INSERT Operation
Following C code segment shows how you can create records in COMPANY table created in the above example −
When the above program is compiled and executed, it will create the given records in COMPANY table and will display the following two lines −
SELECT Operation
Before proceeding with actual example to fetch records, let us look at some detail about the callback function, which we are using in our examples. This callback provides a way to obtain results from SELECT statements. It has the following declaration −
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 statement executed within the SQL argument.
Following C code segment shows how you can fetch and display records from the COMPANY table created in the above example −
When the above program is compiled and executed, it will produce the following result.
UPDATE Operation
Following C code segment shows how we can use UPDATE statement to update any record and then fetch and display updated records from the COMPANY table.
When the above program is compiled and executed, it will produce the following result.
DELETE Operation
Following C code segment shows how you can use DELETE statement to delete any record and then fetch and display the remaining records from the COMPANY table.
When the above program is compiled and executed, it will produce the following result.
Prerequisite:Symbol Table
A Symbol table is a data structure used by the compiler, where each identifier in program’s source code is stored along with information associated with it relating to its declaration. It stores identifier as well as it’s associated attributes like scope, type, line-number of occurrence, etc.
Symbol table can be implemented using various data structures like:
- LinkedList
- Hash Table
- Tree
A common data structure used to implement a symbol table is HashTable.
Operations of Symbol table – The basic operations defined on a symbol table include:
Consider the following C++ function:
// Define a global function
int add(int a, int b)
{
int sum = 0;
sum = a + b;
return sum;
}
Symbol Table for above code:
How To Create Table In Dev C++ Online
Name | Type | Scope |
---|---|---|
add | function | global |
a | int | function parameter |
b | int | function parameter |
sum | int | local |
Below is the C++ implementation of Symbol Table using the concept of Hashing with separate chaining:
#include <iostream> int lineNo; Node() next = NULL; Node(string key, string value, string type, int lineNo) this ->identifier = key; this ->type = type; next = NULL; { cout << 'Identifier's Name:' << identifier << 'Scope: ' << scope << 'Line Number: ' << lineNo << endl; friend class SymbolTable; Node* head[MAX]; public : { head[i] = NULL; bool insert(string id, string scope, string Type, int lineno); bool SymbolTable::modify(string id, string s, { Node* start = head[index]; if (start NULL) if (start->identifier id) { start->type = t; return true ; start = start->next; } // Function to delete an identifier { Node* tmp = head[index]; if (tmp NULL) { } if (tmp->identifier id && tmp->next NULL) { delete tmp; } while (tmp->identifier != id && tmp->next != NULL) { tmp = tmp->next; if (tmp->identifier id && tmp->next != NULL) { tmp->next = NULL; return true ; else { tmp->next = NULL; return true ; return false ; string SymbolTable::find(string id) int index = hashf(id); return '-1' ; while (start != NULL) { if (start->identifier id) { return start->scope; } return '-1' ; // not found bool SymbolTable::insert(string id, string scope, { Node* p = new Node(id, scope, Type, lineno); if (head[index] NULL) { cout << ' } else { while (start->next != NULL) cout << ' } return false ; { asciiSum = asciiSum + id[i]; } // Driver code { string check; if (st.insert( 'if' , 'local' , 'keyword' , 4)) else if (st.insert( 'number' , 'global' , 'variable' , 2)) else check = st.find( 'if' ); cout << 'Identifier Is present' ; cout << 'Identifier Not Present' ; // delete 'if' cout << 'if Identifier is deleted' ; cout << 'Failed to delete' ; // modify 'number' if (st.modify( 'number' , 'global' , 'variable' , 3)) check = st.find( 'number' ); cout << 'Identifier Is present' ; cout << 'Identifier Not Present' ; return 0; |