2012-07-06 5 views
0

SQLite3을 사용 중입니다. 및SQLite3을 사용하여 테이블 행 업데이트

-(IBAction)updateButtonAction:(id)sender 
{ 
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; 
db = [[Database alloc]init]; 
if ([ageTxtFld.text length]>0 && [address1TxtFld.text length]>0 && [address2TxtFld.text length]>0 && [mobileTxtFld.text length]>0 && [classTxtFld.text length]>0) 
{ 
    [appDelegate.updateArray addObject:ageTxtFld.text]; 
    [appDelegate.updateArray addObject:address1TxtFld.text]; 
    [appDelegate.updateArray addObject:address2TxtFld.text]; 
    [appDelegate.updateArray addObject:mobileTxtFld.text]; 
    [appDelegate.updateArray addObject:classTxtFld.text]; 
    NSLog(@"appDelegate.updateArray %@",appDelegate.updateArray); 
    NSString *num = rollNumberTxtFld.text; 
    BOOL is = [db updateTableData:num]; 
    if (is == YES) 
    { 
     updateAlert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Updated" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [updateAlert show]; 
    } 
    else 
    { 
     updateAlert = [[UIAlertView alloc] initWithTitle:@"Fail" message:@"Not Updated" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [updateAlert show]; 
    } 
} 
else 
{ 
    UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Enter all values" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
    [errorAlert show]; 
} 
} 

을 다음과 같이이 메서드를 호출하고 기본 클래스에 다음 코드

- (BOOL)updateTableData :(NSString *) num 
{ 
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; 
    dbPath = [self getDBPath:@"studentslist.sqlite"]; 
    if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) 
    { 
    sqlite3_stmt *updateStmt; 
    const char *sql = "update studentInfo set sAge=?, sAddrs1=?, sAddrs2=?, sMobile =?, sClass =? where sRollNumber=?;"; 
    if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK) 
    { 
     NSLog(@"updateTableData: Error while creating delete statement. '%s'", sqlite3_errmsg(database)); 
     return; 
    } 
    NSLog(@"appDelegate.updateArray %@",appDelegate.updateArray); 
    sqlite3_bind_int(updateStmt, 1,[[appDelegate.updateArray objectAtIndex:0] intValue]); 
    sqlite3_bind_text(updateStmt, 2,[[appDelegate.updateArray objectAtIndex:1] UTF8String], -1,SQLITE_TRANSIENT); 
    sqlite3_bind_text(updateStmt, 3,[[appDelegate.updateArray objectAtIndex:2] UTF8String], -1,SQLITE_TRANSIENT); 
    sqlite3_bind_text(updateStmt, 4,[[appDelegate.updateArray objectAtIndex:3] UTF8String], -1,SQLITE_TRANSIENT); 
    sqlite3_bind_text(updateStmt, 5,[[appDelegate.updateArray objectAtIndex:4] UTF8String], -1,SQLITE_TRANSIENT); 

    if (SQLITE_DONE != sqlite3_step(updateStmt)) 
    { 
     NSLog(@"updateTableData: Error while updating. '%s'", sqlite3_errmsg(database)); 
     sqlite3_finalize(updateStmt); 
     sqlite3_reset(updateStmt); 
     sqlite3_close(database); 
     return NO; 
    } 
    else 
    { 
     NSLog(@"updateTableData: Updated"); 
     sqlite3_finalize(updateStmt); 
     sqlite3_close(database); 
     return YES; 
    } 
} 
else 
{ 
    NSLog(@"updateTableData :Database Not Opened"); 
    sqlite3_close(database); 
    return NO; 
} 

을 사용하여 현재 이미 행을 갱신하지만 항상 NSLog를 인쇄하고 (@ "updateTableData : 업데이트"); 그러나 업데이트되지 않음

누구든지 나를 제안하거나 코드를 사용하여 도움을받을 수 있습니다. 사전에

감사

+1

'getDBPath :'메소드는 앱의 메인 번들 내부를 가리키는 경로를 반환합니다. 그러나 주 번들에 쓸 수는 없습니다. 대신 데이터베이스 파일을 Documents 또는 Library 디렉토리에 저장하십시오. –

답변

0
- (void)createEditableCopyOfDatabaseIfNeeded { 

    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"studentslist.sqlite"]; 
    dbPath =writableDBPath; 
    [databasePath retain]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) return; 
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"studentslist.sqlite"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) { 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
    writableDBPath = nil; 
    documentsDirectory = nil; 
    paths = nil; 
} 

쓰기이 코드에서 방법을 호출합니다. 분명히 이것은 당신의 코드에서 문제였다.

+0

이것을 추가 한 후에도 작동하지 않습니다. –

관련 문제