Contents

Views 14556 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 [pdf] GPS의 동작 원리 ㅎㅎ file hooni 2013.04.23 23834
512 Etc [flash] 자동차 엔진의 원리 (4행정 사이클 그림) file hooni 2013.04.23 21224
511 Develop [doc] UI개발시 유용한 소프트웨어 (개발 및 디버깅 툴) hooni 2013.04.23 12909
510 Develop 모터에 대한 pid 제어.. ㅎㅎ file hooni 2013.04.23 18878
509 Develop 레고 NXT 마인드스톰 밸런싱 로봇 ㅎㅎ file hooni 2013.04.23 57714
508 Etc 아두이노 관련 정보.. hooni 2013.04.23 21770
507 Develop 레고 마인드스톰 NXT 수도쿠, 큐브 소스코드.. 20 file hooni 2013.04.23 81407
506 Develop 밸런싱 로봇.. 최종.. (관련 논문도 첨부) ㅋㅋ file hooni 2013.04.23 22820
505 Develop [js] 자바스크립트(Javascript) 코드를 동적으로 삽입하는 방법.. file hooni 2013.04.23 22877
» Develop [ios] Objective-C에서 SQLite 사용하기.. file hooni 2013.04.23 14556
503 Develop [js] 동적(innerHTML)으로 자바스크립트 실행하기.. 2 file hooni 2013.04.23 18199
502 Develop [js] 양원님이 공유해 주신 유료(5$란다ㅋ) 자료 ㅋㅋ secret hooni 2013.04.23 7948
Board Pagination Prev 1 ... 51 52 53 54 55 56 57 58 59 60 ... 98 Next
/ 98