0

我正在使用在应用程序委托处打开的 sqlite3 数据库,然后在按下按钮后在另一个视图中执行不起作用的更新语句。应用委托代码:

- (BOOL) openDB{
    //NSMutableArray *logoArray = [[NSMutableArray alloc] init];
    @try {
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"SportsLogo.sqlite"];
        BOOL success = [fileMgr fileExistsAtPath:dbPath];
        if(!success)
        {
            NSLog(@"Cannot locate database file '%@'.", dbPath);
        }
        if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
        {
            return NO;
        }
    }
    @catch (NSException *exception) {
            NSLog(@"An exception occured: %@", [exception reason]);
        }
        @finally {
            return YES;
        } 

    }

- (sqlite3 *) getDB{
    return db;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    //MySportsLogosDB *myDB = [[MySportsLogosDB alloc]init];
    //NSMutableArray *myArray = [[NSMutableArray alloc]init];
    if ([self openDB] != YES){
        NSLog(@"Problem with opening the DB");
        return NO;
    }


    return YES;
}

然后,当我按下按钮时,我运行以下代码:

-(IBAction)enter:(id)sender
{
    UIImage *answerImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@",currentTitle, @".png"]];

    if ([[currentTitle lowercaseString] isEqualToString:logoNameText.text]){
        [imageGuessView setImage:answerImage];
        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
      //  NSString *sql =[NSString stringWithFormat:@"select rowid from mytable where name = %@",str];

        NSString *sql = [NSString stringWithFormat:@"UPDATE LogosTbl set logoStatus = ?1 WHERE logoName = '%@'",[currentTitle lowercaseString]];

        sqlite3_stmt *sqlStatement;
        if(sqlite3_prepare_v2([appDelegate getDB], [sql UTF8String], -1, &sqlStatement, NULL) != SQLITE_OK)
        {
            NSLog(@"Problem with prepare statement");
            NSLog(@"%s",sqlite3_errmsg([appDelegate getDB]));
           // printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );

        }

        if(SQLITE_DONE != sqlite3_step(sqlStatement)){
            NSLog(@"Problem with UPDATE");
        }
 }
    else{
        NSLog(@"First No");
    }
}

准备工作正常,步骤也正常,但我没有看到数据已更新。sqlite 文件位于我在 Xcode 中的项目树中,它位于具有读写权限的文件系统中的项目目录中。

谢谢

4

1 回答 1

1

如果数据库文件在项目的包中,那么你不能在那里写任何东西,因为包是只读的。将数据库文件复制到您的文档文件夹,然后您将拥有一个可写的数据库。此示例使用 sqlite3 的 FMDB 包装器:

NSError  *error = nil;
self.nfm = [[NSFileManager alloc] init];

/*
 * if the database isn't in the Documents directory then copy from the bundle
 */

NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsPath = [paths objectAtIndex:0];

NSString *documentsDatabasePath = [documentsPath stringByAppendingPathComponent:@"orders.db"];
if(![self.nfm fileExistsAtPath:documentsDatabasePath]){
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"orders" ofType:@"db"];
    NSLog(@"Model init about to copy %@ to %@",bundlePath,documentsDatabasePath);
    [self.nfm copyItemAtPath:bundlePath toPath:documentsDatabasePath error:&error];
}

self.database = [FMDatabase databaseWithPath:documentsDatabasePath];
if (![self.database open]) {
    if(trace) NSLog(@"Model: start: Could not open database in path \n%@",documentsDatabasePath);
    return nil;
}


if(trace) NSLog(@"Model: start: Opened database in path \n%@",documentsDatabasePath);
于 2012-08-18T10:25:16.037 回答