Contents

Views 14558 Comment 0
Atachment
Attachment '4'
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
먼저 프로젝트를 windows based 로 생성합니다.

그다음에 아래와 같이 프레임워크를 우클릭하여 Add -> Existing Frameworks...을 선택합니다.

1.png

그러면 창이 하나 나올텐데 여기에서 libsqlite3.0.dylib를 선택하여 Add버튼을 클릭합니다.

2.png
 
간단하게 작업은 AppDelegate.m 파일에서만 하도록하겠습니다.

먼저 import 부분에 아래 구문을 추가합니다.
#import <sqlite3.h>

그 다음 아래 메소드에 코드를 추가합니다.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    //도큐먼트 디렉토리 위치를  얻는다.
    NSString *documentsDirectory =
        [NSSearchPathForDirectoriesInDomains(
            NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    //도큐먼트 위치에 db.sqlite명으로 파일패스 설정
    NSString *filePath =
        [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];
    
    //데이터베이스를 연결한다.
    //해당 위치에 데이터베이스가 없을경우에는 생성해서 연결한다.
    sqlite3 *database;
    if (sqlite3_open([filePath UTF8String], &database) != SQLITE_OK) {
        sqlite3_close(database);
        NSLog(@"Error");
    }
    
    
    /*
     테이블 생성
     */
    char *sql = "CREATE TABLE IF NOT EXISTS test 
        (no INTEGER PRIMARY KEY NOT NULL, name VARCHAR)";
    
    //sqlite3_exec 쿼리문을 실행할 수 있다.
    if (sqlite3_exec(database, sql, nil,nil,nil) != SQLITE_OK) {
        
        sqlite3_close(database);
        
        NSLog(@"Error");
    }
    
    
    /*
     insert or update
     */
    sqlite3_stmt *insertStatement;
    char *insertSql = "INSERT or REPLACE INTO test (no,name) VALUES(?,?)";
    
    //프리페어스테이트먼트를 사용
    if (sqlite3_prepare_v2(database, insertSql, -1, &insertStatement, NULL)
        == SQLITE_OK){
        
        //?에 데이터를 바인드
        sqlite3_bind_int(insertStatement, 1, 1);
        sqlite3_bind_text(insertStatement, 2,
            [@"test name" UTF8String],  -1, SQLITE_TRANSIENT);
        
        // sql문 실행
        if (sqlite3_step(insertStatement) != SQLITE_DONE) {
            NSLog(@"Error");
            
        }
    }
    
    
    /*
     select
     */
    sqlite3_stmt *selectStatement;
    
    char *selectSql = "SELECT no, name FROM test";
    
    if (sqlite3_prepare_v2(database, selectSql, -1, &selectStatement, NULL)
        == SQLITE_OK) {
         
        // while문을 돌면서 각 레코드의 데이터를 받아서 출력한다.
        while (sqlite3_step(selectStatement)==SQLITE_ROW) {
            int no = sqlite3_column_int(selectStatement, 0);
            NSString *name =
                [NSString stringWithUTF8String:
                    (char *)sqlite3_column_text(selectStatement, 1) ];
            
            NSLog(@"no : %i, name : %@",no,name);
        }            
        
    }        
    
    
    //statement close
    sqlite3_finalize(insertStatement);
    sqlite3_finalize(selectStatement);
    
    //db close
    sqlite3_close(database);
    
    [window makeKeyAndVisible];
    
    return YES;
}

코드는 간략하게 주석을 달았으니 쉽게 이해할 수 있을 꺼라 생각합니다.

혹시 궁금하신 점있으시면 댓글달아주세요.
제가 아는 범위 또는 찾아볼 수 있는 범위안에서 설명해드리겠습니다.

실행을 하면 디버그콘솔창에 아래와 같은 결과가 나옵니다.
참고로 디버그 콘솔창은 커맨드키 + 쉬프트 + r 입니다.

그리고 소스도 올려드리겠습니다.
(별거는 없습니다 ;;)

3.png
 
[출처] 아이폰에서 sqlite사용하기(초간단 예제)|작성자 만쓰
[출처] http://blog.naver.com/rlarlaks21c/50097250975

?

List of Articles
No. Category Subject Author Date Views
513 Develop [js] 폼(form) 전송시 중복 클릭 방지 간단한 구문 hooni 2013.04.23 9316
512 Develop [c++] 더블 링크리스트(linked list) 학습용 초간단 단어장 file hooni 2003.04.23 9323
511 Develop [c++] MFC로 만든 디렉토리/파일 파인더 file hooni 2013.04.23 9323
510 System/OS [linux] 초간단 Postfix, Covecot, SSL/TLS (SMTP) file hooni 2017.12.11 9323
509 Develop [java] Sieve of Eratosthenes (에라토스테네스의 체) hooni 2013.04.23 9337
508 Develop [c] 다중연결 서버 만들기 #2 - select() 사용 file hooni 2013.04.23 9361
507 Develop [c] 공용체를 이용해 MSB를 LSB로 변환 file hooni 2013.04.23 9374
506 Etc [english] 영어공부 혼자 하기, 인터넷으로 영어공부하기 추천사이트 20선 file hooni 2013.11.25 9408
505 Develop [js] jQuery 셀랙터(selector) 요약 hooni 2013.12.17 9418
504 Develop [java] 스윙(swing)버튼 테스트 ㅋㅋ file hooni 2013.04.23 9421
503 Develop [java] 거스름돈 계산.. swing 사용 file hooni 2013.04.23 9442
502 Develop [c] 팩토리얼.. - 재귀함수 hooni 2003.04.23 9500
Board Pagination Prev 1 ... 51 52 53 54 55 56 57 58 59 60 ... 98 Next
/ 98