SQLite3을 사용해서 만든 아이폰용 입력, 수정, 삭제, 검색 데모 코드 입니다. | 조회 : 45 |
나의 폴더 > 아이폰 | 2012-12-13 (Thu) 11:21 | http://blog.dreamwiz.com/papasmf1/13911303 |
SQLite3을 사용해서 만든 아이폰용 입력, 수정, 삭제, 검색 데모 코드 입니다.
Xcode 4.5 기준으로 다시 작성했습니다.
//
// ViewController.h
// DemoDatabase
//
// Created by JONG DUK KIM on 12. 12. 13..
// Copyright (c) 2012년 JONG DUK KIM. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <sqlite3.h>
@interface ViewController : UIViewController
{
sqlite3 *contactDB;
NSString *databasePath;
}
@property (strong, nonatomic) IBOutlet UITextField *name;
@property (strong, nonatomic) IBOutlet UITextField *jobTitle;
@property (strong, nonatomic) IBOutlet UITextField *phone;
@property (strong, nonatomic) IBOutlet UITextField *address;
@property (strong, nonatomic) IBOutlet UILabel *status;
@property (strong, nonatomic) IBOutlet UILabel *myid;
-(IBAction)saveData:(id)
-(IBAction)updateData:(id)
-(IBAction)deleteData:(id)
-(IBAction)findData:(id)
//키보드 감추기
-(IBAction)textFieldReturn:(id
@end
//
// ViewController.m
// DemoDatabase
//
// Created by JONG DUK KIM on 12. 12. 13..
// Copyright (c) 2012년 JONG DUK KIM. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize name, jobTitle, address, phone;
@synthesize status;
@synthesize myid;
-(IBAction)textFieldReturn:(id
{
[sender resignFirstResponder];
}
-(IBAction)saveData:(id)sender
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB) == SQLITE_OK) {
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO CONTACTS (NAME, TITLE, PHONE, ADDRESS) VALUES ( \"%@\", \"%@\", \"%@\", \"%@\")",name.text, jobTitle.text,
phone.text, address.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
status.text = @"주소록에 추가됨!";
name.text = @"";
jobTitle.text = @"";
address.text = @"";
phone.text = @"";
} else {
status.text = @"주소록 추가에 실패!";
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
-(IBAction)updateData:(id)
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB) == SQLITE_OK) {
NSString *updateSQL = [NSString stringWithFormat:
@"UPDATE CONTACTS SET NAME=\"%@\", TITLE=\"%@\", PHONE=\"%@\", ADDRESS=\"%@\" WHERE ID=%@", name.text, jobTitle.text,
phone.text, address.text, myid.text];
const char *update_stmt = [updateSQL UTF8String];
sqlite3_prepare_v2(contactDB, update_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
status.text = @"주소록 수정됨!";
name.text = @"";
jobTitle.text = @"";
address.text = @"";
phone.text = @"";
} else {
status.text = @"주소록 수정 실패!";
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
-(IBAction)deleteData:(id)
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB) == SQLITE_OK) {
NSString *deleteSQL = [NSString stringWithFormat:
@"DELETE FROM CONTACTS WHERE id=\"%@\"",
myid.text];
const char *delete_stmt = [deleteSQL UTF8String];
sqlite3_prepare_v2(contactDB, delete_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
status.text = @"주소록에서 삭제됨!";
name.text = @"";
jobTitle.text = @"";
address.text = @"";
phone.text = @"";
} else {
status.text = @"주소록 삭제 실패!";
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
-(IBAction)findData:(id)sender
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB) == SQLITE_OK) {
NSString *querySQL = [NSString stringWithFormat:
@"SELECT address, title, phone, id FROM contacts WHERE name=\"%@\"", name.text];
const char *query_stmt = [querySQL UTF8String];
if(sqlite3_prepare_v2(
if(sqlite3_step(statement) == SQLITE_ROW) {
NSString *addressField = [[NSString alloc]
initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
address.text = addressField;
NSString *titleField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(
jobTitle.text = titleField;
NSString *phoneField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(
phone.text = phoneField;
NSString *idField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(
myid.text = idField;
} else {
status.text = @"검색된 내용 없음!";
address.text= @"";
jobTitle.text = @"";
phone.text = @"";
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//데이터베이스와 테이블을 생성한다.
NSString *docsDir;
NSArray *dirPaths;
//Documets 디렉토리 확인하기
dirPaths = NSSearchPathForDirectoriesInDo
docsDir = [dirPaths objectAtIndex:0];
//데이터베이스 파일 경우 구성
databasePath = [[NSString alloc]
initWithString:[docsDir stringByAppendingPathComponent
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 CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, TITLE TEXT, PHONE TEXT, ADDRESS TEXT)";
if(sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
status.text = @"테이블 생성 실패";
}
sqlite3_close(contactDB);
} else {
status.text = @"데이터베이스 오픈 또는 생성 실패!";
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
인터페이스 빌더에서 위와 같이 연결합니다. 실행하면 아래와 같이 실행됩니다. |
댓글 없음:
댓글 쓰기
참고: 블로그의 회원만 댓글을 작성할 수 있습니다.