VC.h
#import <UIKit/UIKit.h>
#import <sqlite3.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *LBL;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *teluguTF;
@property (weak, nonatomic) IBOutlet UITextField *hindi;
@property (weak, nonatomic) IBOutlet UITextField *english;
@property (weak, nonatomic) IBOutlet UITextField *maths;
@property (weak, nonatomic) IBOutlet UITextField *science;
@property (weak, nonatomic) IBOutlet UITextField *social;
- (IBAction)saveBtn:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *findBtn;
- (IBAction)getAllBtn:(id)sender;
- (IBAction)findActionBtn:(id)sender;
@property NSString *myDatabase;
@property sqlite3 *marksDB;
@end
VC.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createElements];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)createElements
{
NSString *docsDir;
NSArray *dirPaths;
dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir=dirPaths[0];
_myDatabase=[[NSString alloc]initWithString:[docsDir stringByAppendingString:@"marks.db"]];
NSLog(@"My Data base %@",_myDatabase);
NSFileManager *fileMgr=[NSFileManager defaultManager];
if ([fileMgr fileExistsAtPath:_myDatabase]==NO)
{
const char *dbpath=[_myDatabase UTF8String];
if (sqlite3_open(dbpath, &_marksDB)==SQLITE_OK)
{
char *errMsg;
const char *sql_stmt="CREATE TABLE IF NOT EXISTS MARKS(ID INTEGER PRIMARY KEY AUTOINCREMENT ,NAME TEXT,TELUGU INTEGER,HINDI INTEGER,ENGLISH INTEGER,MATHS INTEGER,SCIENCE INTEGER,SOCIAL INTEGER )";
if (sqlite3_exec(_marksDB, sql_stmt, NULL, NULL, &errMsg)!=SQLITE_OK)
{
_LBL.text=@"Failed to create Table";
}
sqlite3_close(_marksDB);
}
else
{
_LBL.text=@"Failed to Create/Open Database";
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveBtn:(id)sender {
sqlite3_stmt *statement;
const char *dbpath=[_myDatabase UTF8String];
if (sqlite3_open(dbpath, &_marksDB)==SQLITE_OK)
{
NSString *insertSQL=[NSString stringWithFormat:@"INSERT INTO MARKS(name,telugu,hindi,english,maths,science,social)VALUES(\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",_nameTextField.text,_teluguTF.text,_hindi.text,_english.text,_maths.text,_science.text,_social.text];
const char *insert_stmt=[insertSQL UTF8String];
sqlite3_prepare_v2(_marksDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement)==SQLITE_DONE)
{
_LBL.text=@"Contact Added";
_nameTextField.text=@"";
_teluguTF.text=@"";
_hindi.text=@"";
_english.text=@"";
_maths.text=@"";
_science.text=@"";
_social.text=@"";
}
else
{
_LBL.text=@"Failed to Add Contact";
}
sqlite3_finalize(statement);
sqlite3_close(_marksDB);
}
}
- (IBAction)getAllBtn:(id)sender {
}
- (IBAction)findActionBtn:(id)sender {
const char *dbpath=[_myDatabase UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_marksDB)==SQLITE_OK)
{
NSString *query=[NSString stringWithFormat:@"SELECT telugu,hindi,english,maths,science,social FROM MARKS WHERE name=\"%@\"",_nameTextField.text];
const char *query_stmt=[query UTF8String];
if (sqlite3_prepare_v2(_marksDB, query_stmt, -1, &statement, NULL)==SQLITE_OK)
{
if (sqlite3_step(statement)==SQLITE_ROW)
{
NSString *teluguMarks=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
_teluguTF.text=teluguMarks;
NSString *hindiMarks=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
_hindi.text=hindiMarks;
NSString *englishMarks=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
_english.text=englishMarks;
NSString *mathsMarks=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)];
_maths.text=mathsMarks;
NSString *scince=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 4)];
_science.text=scince;
NSString *social=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 5)];
_social.text=social;
_LBL.text=@"Match Found";
}
else
{
_LBL.text=@"Not Matched";
_nameTextField.text=@"";
_teluguTF.text=@"";
_hindi.text=@"";
_english.text=@"";
_maths.text=@"";
_science.text=@"";
_social.text=@"";
}
sqlite3_finalize(statement);
}
sqlite3_close(_marksDB);
}
}
@end
Sign up here with your email
ConversionConversion EmoticonEmoticon