Monday, 28 September 2015

how to use sqlite database


Key SQLite Functions 


sqlite3_open() - Opens specified database file. If the database file does not already exist, it is created. 


sqlite3_close() – Closes a previously opened database file. 


sqlite3_prepare_v2() – Prepares a SQL statement ready for execution. 


sqlite3_step() – Executes a SQL statement previously prepared by the sqlite3_prepare_v2() function. 

sqlite3_column_<type>() – Returns a dat
a field from the results of a SQL retrieval operation where <type> is replaced by the data type of the data to be extracted (text, blob, bytes, int, int16 etc).


sqlite3_finalize() - Deletes a previously prepared SQL statement from memory. 

sqlite3_exec() – Combines the functionality of sqlite3_prepare_v2()
, sqlite3_step() and sqlite3_finalize() into a single function call.

 -----------------------------------------------
    Preparing an iPhone Application Project for                SQLite Integration 
Declaring a SQLite Database
sqlite3 *contactDB; //Declare a pointer to sqlite database structure

Opening or Creating a Database

int sqlite3_open(const char *filename, sqlite3 **database);

const char *dbpath = [databasePath UTF8String]; // Convert 

 NSString to UTF-8

       
sqlite3_open(dbpath, &contactDB) == SQLITE_OK

Preparing a SQL Statement

NSString *querySQL = @"SELECT address, phone FROM

 contacts”;

const char *query_stmt = [querySQL UTF8String];

sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK


Step To Access DataBase
1) sqlite3 *databaseObj;
2) DataBase Path
3) sqlite3_open
([path UTF8String],&databaseObj) == SQLITE_OK
4) sqlite3_prepare_v2
(databaseObj,[query UTF8String],-1, &statement, NULL)) == SQLITE_OK
5) sqlite3_step
(statement) == SQLITE_ROW
6) sqlite3_finalize(statement)
7) sqlite3_close
(databaseObj) == SQLITE_OK