app link: https://drive.google.com/open?id=0BwU5Y4Xqfa42RHJ3UUhoSWRLcU0
view.h
#import <UIKit/UIKit.h>
#import <sqlite3.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *addressTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
- (IBAction)saveData:(id)sender;
- (IBAction)findContact:(id)sender;
@property NSString * dataBasePath;
@property sqlite3 * contactsDB;
@end
view.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
_dataBasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
@"contacts.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _dataBasePath ] == NO)
{
const char *dbpath = [_dataBasePath UTF8String];
if (sqlite3_open(dbpath, &_contactsDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXIST CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
if (sqlite3_exec(_contactsDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
_statusLabel.text = @"Failed to create table";
}
sqlite3_close(_contactsDB);
} else {
_statusLabel.text = @"Failed to open/create database";
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveData:(id)sender {
sqlite3_stmt *statement;
const char *dbpath = [_dataBasePath UTF8String];
if (sqlite3_open(dbpath, &_contactsDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")",
_nameTF.text, _addressTF.text, _phoneTF.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_contactsDB, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
_statusLabel.text = @"Contact added";
_nameTF.text = @"";
_addressTF.text = @"";
_phoneTF.text = @"";
} else {
_statusLabel.text = @"Failed to add contact";
}
sqlite3_finalize(statement);
sqlite3_close(_contactsDB);
}
}
- (IBAction)findContact:(id)sender {
const char *dbpath = [_dataBasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_contactsDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
@"SELECT address, phone FROM contacts WHERE name=\"%@\"",
_nameTF.text];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_contactsDB,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *addressField = [[NSString alloc]
initWithUTF8String:
(const char *) sqlite3_column_text(
statement, 0)];
_addressTF.text = addressField;
NSString *phoneField = [[NSString alloc]
initWithUTF8String:(const char *)
sqlite3_column_text(statement, 1)];
_phoneTF.text = phoneField;
_statusLabel.text = @"Match found";
} else {
_statusLabel.text = @"Match not found";
_addressTF.text = @"";
_phoneTF.text = @"";
}
sqlite3_finalize(statement);
}
sqlite3_close(_contactsDB);
}
}
@end
Sign up here with your email
ConversionConversion EmoticonEmoticon