#include "/user/include/sqlite3.h"

-(NSString *) dataPath
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
return [documentDirectory stringByAppendingFormat:@"data.sqlite3""];
}

//applicationDidFinishLaunching 에서 호출 . Table 이 없으면 생성
-(void)createData{
if(sqlite3_open([[self dataPath] UTF8Strin],  &database) != SQLITE_OK){
sqlite3_close(database);
}

char *createSQL = "Create table if not exists address(name text, addr text, tel text, memo text);";

//DDL
if(sqlite3_exec(database, createSQL) != SQLITE_OK)
{
sqlite3_close(database);
}
sqlite3_close(database);
}

//화면이 갱신될때마다 호출
-(void)viewWillAppear:(BOOL) animated{
array = [[NSMutableArray alloc] init];

if(sqlite3_open([[self dataPath] UTF8String], &databae) != SQLITE_OK){
sqlite3_close(database);
}

NSString *selectSQL = @"select * from address";
sqlite3_stmt *stmt; //SQL구문을 컴파일한 바이너리 정보를 담은 구조체

//DML
//sqlite3_prepare_v2 : SQL문을 컴파일해서 sqlite3_stmt 로 만들어줌
if(sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, stmt, nil) == SQLITE_OK){
while(sqlite3_step(stmt) == SQLITE_ROW){
char *nameData = (char *)sqlite3_column_text(stmt, 0);
char *addrData = (char *)sqlite3_column_text(stmt, 1);
char *telData = (char *)sqlite3_column_text(stmt, 2);
char *memoData = (char *)sqlite3_column_text(stmt, 3);

NSString *nameValue = [[NSString alloc] initWithUTF8String:nameData];
NSString *addrValue = [[NSString alloc] initWithUTF8String:addrData];
NSString *telValue = [[NSString alloc] initWithUTF8String:telData];
NSString *memoValue = [[NSString alloc] initWithUTF8String:memoData];

NSDictionary *row = [[NSDictionary alloc] initWithObjectAndKeys:nameValue, @"name", addrValue, @"addr",  telValue, @"tel", memoValue, @"memo", nil];
[array addObject:row];
[row release];
[nameValue release];
[addrValue release];
[telValue  release];
[memoValue release];

}
sqlite3_finalize(stmt);
}
sqlite3_close(database);
[self.tableView reloadData];

[super viewWillAppear:animated];
}

//EditController.m
-(void)viewWillAppear:(BOOL)animated{
txtName.text = @"";
txtAddr.text = @"";
txtTel.text = @"";
txtMemo.text = @"";

if([sMode isEqualToString:@"편집"]){
if(sqlite3_open([[self dataPath] UTF8String], &database) != SQLITE_OK){
sqlite3_close(database);
}

char *selectSQL = "select * from address where name = ? ";
sqlite3_stmt *stmt;

if(sqlite3_prepare_v2(adtabase, selectSQL, -1, stmt, nil) == SQLITE_OK){
sqlite3_bind_text(stmt, 1, [sName UTF8String], -1,  SQLITE_TRANSIENT);

while(sqlite3_step(stmt) == SQLITE_ROW){
char *nameData = (char *)sqlite3_column_text(stmt, 0);
char *addrData = (char *)sqlite3_column_text(stmt, 1);
char *telData = (char *)sqlite3_column_text(stmt, 2);
char *memoData = (char *)sqlite3_column_text(stmt, 3);

NSString *nameValue = [[NSString alloc] initWithUTF8String:nameData];
txtName.text = nameValue;
[nameData release];

NSString *addrValue = [[NSString alloc] initWithUTF8String:addrData];
txtAddr.text = addrValue;
[addrData release];

....
....
}
sqlite3_finallize(stmt);
}
sqlite3_close(database);
}
[super viewWillAppear:animated];
}

-(void)Save{
if(sqlite3_open([[self dataPath] UTF8String], &database) != SQLITE_OK){
sqlite3_close(database);
}

if([sMode isEqualToString:@"추가"]){ 
char *insertSQL = "insert into address(name, addr, tel, memo) values (? , ?, ?, ?);";
sqlite3_stmt *stmt;

if(sqlite3_prepare_v2(database, insertSQL, -1, stmt, nil) == SQLITE_OK)
{
sqlite3_bind_text(stmt, 1, [txtName.text UTF8String], -1_;
sqlite3_bind_text(stmt, 2, [txtAddr.text UTF8String], -1_;
sqlite3_bind_text(stmt, 3, [txtTel.text UTF8String], -1_;
sqlite3_bind_text(stmt, 4, [txtMemo.text UTF8String], -1_;
}

if(sqlite3_step(stmt) != SQLITE_DONE){
sqlite3_close(database);
}

sqlite3_finalize(stmt);
}
else if([sMode isEqualToString:@"편집"]){
NSString *updateSQL = [NSString stringWithFormat:@"Update address name = '%@', addr = '%@', tel = '%@', memo = '%@' where name = '%@'",
txtName.text, txtAddr.text, txtTel.text, txtMemo.text, sName];

sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(database, updateSQL, -1, stmt, nil) != SQLITE_OK)
{
sqlite3_close(database);

if(sqlite3_step(stmt) != SQLITE_DONE){
sqlite3_close(database);
}

sqlite3_finalize(stmt);
}

sqlite3_close(database);
[self.navigationController popViewControllerAnimated:YES];
}

'Object-C' 카테고리의 다른 글

MapView  (0) 2010.10.09
XML  (0) 2010.10.09
SQLITE  (0) 2010.10.08
CoreData  (0) 2010.10.07
archiving  (0) 2010.10.07
pickerView  (0) 2010.10.07

+ Recent posts