2011-02-03 3 views
0

이 문제를 이해할 수 없습니다.Insert가 SQLite iOS에서 작동하지 않습니다.

- (void)viewDidLoad { 
    NSString *docsDir; 
    NSArray *dirPaths; 

    // Get the documents directory 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    docsDir = [dirPaths objectAtIndex:0]; 

    // Build the path to the database file 
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.sqlite"]]; 
} 

- (IBAction) saveData{ 
    sqlite3_stmt *statement; 

    const char *dbpath = [databasePath UTF8String]; 
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
    { 
     NSLog(@"enter %@",name.text); 

     NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO EMP (name) VALUES (\"%@\")", name.text]; 

     //NSLog(@"enter123 %@",dbpath); 

     const char *insert_stmt = [insertSQL UTF8String]; 

     sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL); 
     NSLog(@"enter789 "); 

     if (sqlite3_step(statement) == SQLITE_DONE) 
     { 
      NSLog(@"Done success"); 

      /*status.text = @"Contact added"; 
      name.text = @""; 
      address.text = @""; 
      phone.text = @"";*/ 
     } 
     else 
     { 
      NSLog(@"fail"); 
      //status.text = @"Failed to add contact"; 
     } 
     sqlite3_finalize(statement); 
     sqlite3_close(contactDB); 
    } 
} 
+2

어느 것 다른 사람이 귀하의 질문에 그대로 남아있는 경우. 질문을 수정하고보고있는 오류나 경고에 대한 세부 정보를 추가하십시오. 일어날 것으로 예상되는 것과 실제로 발생하는 것을 설명하고 문제를 해결하기 위해 이미 시도한 것을 보여주십시오. 이 문제가 없으면 질문이 매우 빨리 닫힙니다. – Jasarien

+0

정확한 문제는 무엇입니까? 어떤 충돌? 또는 실패? – KingofBliss

+0

** 항상 ** 모든 SQLite 작업의 반환 코드를 확인하십시오. 그리고 연산이 실패하면 ** 항상'sqlite3_errmsg'를 호출하고 결과 메시지를 출력하십시오. –

답변

-1

사용이 그것의 도움 당신

sqlite3_stmt *statement; 
    const char *dbpath = [databasePath UTF8String]; 
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK){ 
    const char *insert_stmt = "INSERT INTO EMP (name) VALUES (?)"; 
    if(sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL)==SQLITE_OK){ 
     sqlite3_bind_text(statement,1, [name.text UTF8String], -1, SQLITE_TRANSIENT); 
    } 
    if (sqlite3_step(statement) == SQLITE_DONE) 
     NSLog(@"You have Sucessfully saved!"); 
    else 
     NSLog(@"Failed to Save!"); 

     sqlite3_finalize(statement); 
    } 
+0

적절한 들여 쓰기가 큰 도움이됩니다. –

+0

그리고 준비가 실패하더라도 왜이 단계를 실행합니까? –

관련 문제