SQLite Database
Database Demo For Begineer.......
Create SQLite database in iOS .....
1. Open the Build Phases tab, and within Link Binary with Libraries, add the following frameworks: -libsqlite3.dylib
2. create Sqlite3 Object In your view .h file for using like that ..
#import <sqlite3.h>
@property (nonatomic) sqlite3 *contactDB;
Add Your Database to Bundle :
go to Build Phase > Copy Bundle Resources
we create a whole apps for Save,Find And Update in to the database . so we first create require
` UITextFiled`object in`viewcontroller.h` file. |
Here we create Three UITextField object name,address,and phone. And one UILabel object status. same as we create three button Action in this file. And don't forget define NSString object for databasepath.
@interface ViewController : UIViewController@property (nonatomic) sqlite3 *contactDB; //for path@property (strong, nonatomic) NSString *databasePath; // userdata get@property (strong, nonatomic) IBOutlet UITextField *name;@property (strong, nonatomic) IBOutlet UITextField *address;@property (strong, nonatomic) IBOutlet UITextField *phone;@property (strong, nonatomic) IBOutlet UILabel *status; //btn action- (IBAction)saveData:(id)sender;- (IBAction)findContact:(id)sender;- (IBAction)updateContact:(id)sender;@end
Then set your view like this.. and mapping your all Lable and TextField and set Action.
In
"viewcontroller.m" File we create database first and also find the path using "NSSearchpathfordocumentdirectory" method . In "viewdidload" method put This code.This Is Copy to ViewController.M (viewdidload method) :
NSString *docsDir;
NSArray *dirPaths ;
dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); // Get the documents directory
docsDir = dirPaths[0];
_databasepath = [[NSString alloc]initWithString: [docsDir stringByAppendingPathComponent:@"MyData.sqlite"]]; // Build the path to the database file
This code is useful for the copy file NSBundle to the NSDocument directory folder. But First Check the file is available in bundle or not ..?
This Is Copy to AppDelegate.M :-
NSString *pathsToReources = [[NSBundle mainBundle] resourcePath];
NSString *yourOriginalDatabasePath = [pathsToReources stringByAppendingPathComponent:@"MyData.sqlite"];
NSArray *pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [pathsToDocuments objectAtIndex: 0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"MyData.sqlite"];
if (![[NSFileManager defaultManager] isReadableFileAtPath: dbPath]) {
if ([[NSFileManager defaultManager] copyItemAtPath: yourOriginalDatabasePath toPath: dbPath error: NULL] != YES)
NSAssert2(0, @"Fail to copy database from %@ to %@", yourOriginalDatabasePath, dbPath);
}
Then This Show The Operation of the Data........
Save :-
-(IBAction)Save:(id)sender
{
sqlite3_stmt *statement;
const char *dbpath = [_databasepath UTF8String];
if (sqlite3_open(dbpath, &_connectDB) == SQLITE_OK)
{ NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO data (name, address) VALUES (\"%@\", \"%@\")",self.name.text, self.address.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_connectDB, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!" message:@"Data Saved"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
} else {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!" message:@"Data Not Saved"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
sqlite3_finalize(statement);
sqlite3_close(_connectDB);
}
}
Search :-
-(IBAction)Search:(id)sender
{
const char *dbpath = [_databasepath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_connectDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
@"SELECT address FROM data WHERE name=\"%@\"",
_name.text];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_connectDB, 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)];
_address.text = addressField;
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!" message:@"Data Seached"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
} else {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!" message:@"Data Not Searched"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
sqlite3_finalize(statement);
}
sqlite3_close(_connectDB);
}
}
Update :-
-(IBAction)Update:(id)sender
{
sqlite3_stmt *statement;
const char *dbpath = [_databasepath UTF8String];
if (sqlite3_open(dbpath, &_connectDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:
@"Update data Set address = \"%@\" Where name= \"%@\"",self.address.text,self.name.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_connectDB, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!" message:@"Data Updated"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
} else {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!" message:@"Data Not Updated"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
sqlite3_finalize(statement);
sqlite3_close(_connectDB);
}
}
Delate :-
-(IBAction)drop:(id)sender
{
const char *dbpath = [_databasepath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_connectDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
@"Delete From data WHERE name=\"%@\"",
_name.text];
const char *query_stmt = [querySQL UTF8String];
sqlite3_prepare_v2(_connectDB,
query_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!" message:@"Data is Deleted"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
sqlite3_finalize(statement);
}
sqlite3_close(_connectDB);
}
&&&&&&&&&&&&&& AT LAST
Database Copy In Simulator Path.....

