The third parameter in Sqlite_exec () mentioned earlier, SQLite calls this callback function for each record that is processed in each SELECT statement executed within the SQL parameter.
This section adds two functions, selectfromtable and updatetable.
The instance program is as follows:
#include <stdio.h> #include <stdlib.h> #include "sqlite/sqlite3.h" #define Db_nane "Sqlite/test.db" sqlite3 *db = null;char* sql = Null;char *zerrmsg = null;const char* data = "Callback function called";/* Change to global */int ret = 0;type Def enum{false, true} bool;/*typedef int (*sqlite3_callback) (void*, Data provided in the 4th argument of Sqlite3 _exec () int, the number of columns in rowchar**, a array of strings representing fields in the rowchar** an ARRA Y of strings representing column names); */static int callback (void *notused, int argc, char **argv, char **azcolname) { int i = 0; for (i=0; i < argc; i++) {printf ("%s =%s\n", Azcolname[i], argv[i]? Argv[i]: "NULL"); } printf ("\ n"); return 0;} BOOL Connectdb () {ret = Sqlite3_open (Db_nane, &db); if (ret! = SQLITE_OK) {fprintf (stderr, "Error Open Database:%s\n", sqlite3_errmsg (db)); Sqlite3_free (ZERRMSG); return false; } fprintf (StdoUT, "successfully opened database\n"); return true;} BOOL CreateTable () {/* Create SQL statement */sql = "Create TABLE Company (" "ID INT PRIMARY KEY not NULL," "NAME TEXT NOT NULL," "The Age INT is not null," "ADDRESS CHAR (#)," "SALARY REAL); "; /* Execute SQL statement */ret = sqlite3_exec (db, SQL, callback, 0, &zerrmsg); if (ret! = SQLITE_OK) {fprintf (stderr, "Error SQL:%s\n", zerrmsg); Sqlite3_free (ZERRMSG); return false; } fprintf (stdout, "successfully table created\n"); return true;} BOOL Insertrecords () {/* Create SQL statement */sql = "INSERT into Company" (id,name,age,address,salary) "VAL UES (1, ' Paul ', +, ' California ', 20000.00); "INSERT into Company" VALUES (2, ' Allen ', id,name,age,address,salary, ' Texas ', 15000.00); "" INSERT into Company (id,name,age,address,salary) "VALUES (3, ' Teddy ', +, ' NoRway ', 20000.00); "" INSERT into Company (id,name,age,address,salary) "VALUES (4, ' Mark ', +, ' Rich-mond ', 65000.00);"; /* Execute SQL statement */ret = sqlite3_exec (db, SQL, callback, 0, &zerrmsg); if (ret! = SQLITE_OK) {fprintf (stderr, "SQL Error:%s\n", zerrmsg); Sqlite3_free (ZERRMSG); return false; } fprintf (stdout, "successfully records created\n"); return true;}
BOOL Selectfromtable ()/* new plus */{/ * Create SQL statement */ sql = "SELECT * from company"; /* Execute SQL statement * /ret = sqlite3_exec (db, SQL, Callback, (void*) data, &zerrmsg); if (ret! = SQLITE_OK) { fprintf (stderr, "Error SQL:%s\n", zerrmsg); Sqlite3_free (zerrmsg); return false; } fprintf (stdout, "successfully operation Done\n"); return true;}
BOOL UpdateTable ()/* NEW Add */{/* Create merged SQL statement */sql = "UPDATE Company set SALARY = 25000.00 where id=1 ; "SELECT * from Company"; /* Execute SQL statement */ret = sqlite3_exec (db, SQL, Callback, (void*) data, &zerrmsg); if (ret! = SQLITE_OK) {fprintf (stderr, "SQL Error:%s\n", zerrmsg); Sqlite3_free (ZERRMSG); return false; } fprintf (stdout, "successfully operation done \ n"); return true;}
BOOL Closedb () { int ret = 0; ret = sqlite3_close (db); if (ret = = sqlite_busy) { return false; } return true;} int main (int argc, char* argv[]) { connectdb (); /*createtable (); */ /*insertrecords (); */ selectfromtable (); UpdateTable (); Selectfromtable (); Closedb (); return 0;}