Database(Automatically Create a Database)
dbacc.h
#import <UIKit/UIKit.h>
#import <sqlite3.h>
@interface dbacc : UIViewController
{
sqlite3 *contactDB;
NSString *databasePath;
NSString *transfer_name;
NSString *transfer_amount;
}
@property(nonatomic,retain)NSString *transfer_name;
@property(nonatomic,retain)NSString *transfer_amount;
- (IBAction) saveData;
- (NSMutableArray*)getdata;
- (IBAction)deletedata;
@end
dbacc.m
#import "dbacc.h"
@implementation dbacc
@synthesize transfer_name,transfer_amount;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void) saveData
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO account (name,amount) VALUES (\"%@\",\"%@\")", transfer_name,transfer_amount];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"inserted");
}
else
{
NSLog(@"failed");
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
- (NSMutableArray*)getdata
{
NSString *docsdir;
NSArray *dirpaths;
dirpaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsdir=[dirpaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString: [docsdir stringByAppendingPathComponent:
@"christmas.db"]];
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSMutableArray *products = [[[NSMutableArray alloc] init] autorelease];
char const *sql="select * from favour";
sqlite3_stmt *statement;
int sqlResult = sqlite3_prepare_v2(contactDB, sql, -1, & statement, NULL);
if ( sqlResult== SQLITE_OK)
{
// Step through the results - once for each row.
while (sqlite3_step(statement) == SQLITE_ROW)
{
//fan1 *pdt=[[fan1 alloc]init];
char *name = (char *)sqlite3_column_text(statement, 1);
char *amount = (char *)sqlite3_column_text(statement, 2);
//pdt.name = (name) ? [NSString stringWithUTF8String:name] :@"";
// pdt.image = (image) ? [NSString stringWithUTF8String:image] :@"";
[products addObject:pdt];
[pdt release];
}
sqlite3_finalize(statement);
}
else {
NSLog(@"Problem with the database:");
NSLog(@"%d",sqlResult);
}
return products;
}
}
- (IBAction)deletedata
{
NSString *docsdir;
NSArray *dirpaths;
dirpaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsdir=[dirpaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString: [docsdir stringByAppendingPathComponent:
@"christmas.db"]];
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM account WHERE name='%@'",transfer_delete];
const char *delete_stmt = [deleteSQL UTF8String];
sqlite3_prepare_v2(contactDB, delete_stmt, -1, &statement, NULL);
}
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"inserted");
}
else
{
NSLog(@"failed");
}
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
NSString *docsdir;
NSArray *dirpaths;
dirpaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsdir=[dirpaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString: [docsdir stringByAppendingPathComponent:
@"bank.db"]];
// NSFileManager *filemgr = [NSFileManager defaultManager];
// if ([filemgr fileExistsAtPath: databasePath ] == NO)
// {
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS account (NAME TEXT,AMOUNT TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
//status.text = @"Failed to create table";
NSLog(@"Failed to create table");
}
sqlite3_close(contactDB);
} else {
//status.text = @"Failed to open/create database";
NSLog(@"Failed to open/create database");
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(void)dealloc
{
[transfer_name release];
[transfer_amount release];
[super dealloc];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Credit.h
@interface Credit : UIViewController
{
IBOutlet UITextField *name;
IBOutlet UITextField *amount;
}
-(IBAction)Credit:(id)sender;
@end
Credit.m
-(IBAction)Credit:(id)sender
{
dbacc *db=[[dbacc alloc]init];
db.transfer_name=name.text;
db.transfer_amount=amount.text;
[db viewDidLoad];
[db saveData];
[db getdata];
[db deletedata];
[db release];
}
Credit.xib
1.map the values to be name,amount,saveData,getdata,deletedata.
2.Add the two frameworks are libsqlite3.0.dylib,libsqlite3.dylib
No comments:
Post a Comment