2012-07-06 49 views
0

正在使用SQLite3。和正在更新行中已经存在通过在主类使用以下代码使用SQLite3更新表格行

- (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; 
} 

如下

-(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]; 
} 
} 

正在调用该方法,但它总是打印的NSLog(@“updateTableData:更新”);但不更新

任何人都可以建议我或帮助代码。

由于提前

+1

我确定你的'getDBPath:'方法返回一个指向你的应用程序主包中的路径。但是你不能写入主包;请将您的数据库文件存储在文档或库目录中。 – 2012-07-06 09:49:43

回答

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

也添加此后不工作 – 2012-07-06 10:43:00