2013-07-22 3 views
0

안녕하세요 저는 sqlitedb를 사용하는 앱을 개발하고 있습니다. 몇 가지 예를 읽고 내 앱을 만들려고했습니다. 삽입하려는 테이블에 3 개의 열이 있습니다. ID INTEGER 기본 키 자동 침입, 이름 텍스트, 실제 시간, 실제 시간 읽은 예제를 기반으로 아래 코드를 작성합니다. 하지만 항상 레코드를 추가하지 못했습니다. 추신 : 나는 db 생성을 테스트했다. 나는 이것을 tuterial에 사용했는데 다른 응용 프로그램에서는 괜찮 았습니다. 직접 쓸 수 없습니다 자원 folder.it에 database에 데이터를 쓰고 있기 때문에 여기에 먼저 document directorydatabase을 복사해야iphone에서 sqlite에 데이터를 삽입 (& 실패)하려고합니다.

 NSTimeInterval endTime = [[NSDate date] timeIntervalSinceReferenceDate]; 

    double elapsedTime = endTime-startcache; 

    NSString *my=[NSString stringWithFormat:@"%f",elapsedTime]; 
    TextField1.text=my; 

    // saving in database 
    NSString * durationstring=[NSString stringWithFormat:@"%f",elapsedTime]; 
    NSString * timestring=[NSString stringWithFormat:@"%f",endTime]; 

    sqlite3_stmt *statement; 
    const char *dbpath = [_databasePath UTF8String]; 

    if (sqlite3_open(dbpath, &_activityDB) == SQLITE_OK) 
    { 

     NSString *insertSQL = [NSString stringWithFormat: 
           @"INSERT INTO ACTIVITYTABLE (name, duration,time) VALUES (\"%@\", \"%@\", \"%@\")", 
           activityname, durationstring,timestring]; 

     const char *insert_stmt = [insertSQL UTF8String]; 
     sqlite3_prepare_v2(_activityDB, insert_stmt, 
          -1, &statement, NULL); 
     if (sqlite3_step(statement) == SQLITE_DONE) 
     { 
      self.status.text = @"record added"; 


     } else { 
      self.status.text = @"Failed to add record"; 
     } 
     sqlite3_finalize(statement); 
     sqlite3_close(_activityDB); 
    } 
+0

내가이 튜토리얼 http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_6_iPhone_Application는 괜찮아요을 사용하지만 난 다른 데이터 유형을 시도 할 때 나는 –

답변

1

그것의 작동 나던.

NSError *err=nil; 
    NSFileManager *fm=[NSFileManager defaultManager]; 

    NSArray *arrPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, -1); 
    NSString *path=[arrPaths objectAtIndex:0]; 
    NSString path2= [path stringByAppendingPathComponent:@"ProductDatabase.sql"]; 


    if(![fm fileExistsAtPath:path2]) 
    { 

     bool success=[fm copyItemAtPath:databasePath toPath:path2 error:&err]; 
     if(success) 
      NSLog(@"file copied successfully"); 
     else 
      NSLog(@"file not copied"); 

    } 

및 코드에서 바인드 문을 제거하고 당신이 아이폰 응용 프로그램에서 데이터를 관리하는 SQLite는 사용하는 방법을 학습에 정말 관심이 있다면

if(sqlite3_prepare_v2(database, insert_stmt, -1, &statement, nil)== SQLITE_OK) 
     { 


     if(sqlite3_step(statement)==SQLITE_DONE) 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Product Added" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  
      [alert show]; 

     } 
     else 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Product Not Added" message:@"An error has occured" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
      alert=nil; 
     } 
} 

로 변경, 난 당신을 추천 할 것입니다

http://www.icodeblog.com/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/

튜토리얼은 선택, 업데이트, 삽입 및 드를 처리 : 그것은 매우 좋은 소개의로, 다음 자습서를 완료 SQLite를 사용하여 iPhone 앱에서 데이터를 가져온다.

+0

답장을 보내 주셔서 감사합니다 귀하의 제안 링크 오류에 직면했다. 나는 그것을 조사해야한다. 하지만 나는이 tutrial http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_6_iPhone_Application을 사용하고 그것은 작동하지만 내가 데이터 타입을 변경했을 때 나는 에러에 직면했다. –

관련 문제