// # Import "/usr/include/sqlite3.h" // Add this to your header of code
-( Void ) Savedata
{
Sqlite3_stmt * statement;
Const Char * Dbpath = [databasepath utf8string];
If (Sqlite3_open (dbpath, & contactdb) = sqlite_ OK)
{
Nsstring * insertsql = [nsstring stringwithformat: @" Insert into contacts (name, address, phone) values (\ " % @\ " , \ "% @ \", \ "% @\") " , Name. Text, address. Text, phone. Text];
Const Char * Insert_stmt = [insertsql utf8string];
Sqlite3_prepare_v2 (contactdb, insert_stmt ,- 1 , & Statement, null );
If (Sqlite3_step (statement) = sqlite_done)
{
Self. Status. Text = @" Contact added " ;
Self. Name. Text = @"" ;
Self. Address. Text = @"" ;
Self. Phone. Text = @"" ;
} Else {
Self. Status. Text = @" Failed to add contact " ;
}
Sqlite3_finalize (statement );
Sqlite3_close (contactdb );
}
}
-( Void ) Findcontact
{
Const Char * Dbpath = [databasepath utf8string];
Sqlite3_stmt * statement;
If (Sqlite3_open (dbpath, & contactdb) = sqlite_ OK)
{
Nsstring * querysql = [nsstring stringwithformat: @" Select address, phone from contacts where name = \ " % @\ "" , Name. Text];
Const Char * Query_stmt = [querysql utf8string];
If (Sqlite3_prepare_v2 (contactdb, query_stmt ,- 1 , & Statement, null) = sqlite_ OK)
{
If (Sqlite3_step (statement) = sqlite_row)
{
Nsstring * addressfield = [[nsstring alloc] initwithuf8string :( Const Char *) Sqlite3_column_text (statement, 0 )];
Self. Address. Text = addressfield;
Nsstring * phonefield = [[nsstring alloc] initwithuf8string :( Const Char *) Sqlite3_column_text (statement, 1 )];
Self. Phone. Text = phonefield;
Self. Status. Text = @" Match found " ;
} Else {
Self. Status. Text = @" Match not found " ;
Self. Address. Text = @"" ;
Self. Phone. Text = @"" ;
}
Sqlite3_finalize (statement );
}
Sqlite3_close (contactdb );
}
}