0

我总是使用这段代码在我的 iPhone 应用程序中打开一个 sqlite 数据库,但现在使用 iOS6,这段代码不起作用。功能: sqlite3_prepare_v2 失败,但我不明白为什么!

我也不明白为什么如果我尝试用另一个不存在文件的名称更改 sqlite db 的名称:“gamer.sqlite”,函数:'sqlite3_open' 再次返回 SQLITE_OK 值。

这是我的代码:

-(NSMutableArray*)view_table
{
    const char *sql;

    NSMutableArray *content=[[NSMutableArray alloc] initWithObjects:nil];
    [self msgbox:@"eseguo"];
    /*elementi da visualizzare nella tabella*/
    if(sqlite3_open([databasePath  UTF8String],&database)==SQLITE_OK)
    {
            [self msgbox:@"opened"];
        sql = "SELECT * FROM  list_gamer";
        if (sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL) == SQLITE_OK)
        {
              [self msgbox:@"query eseguita"];
            while(sqlite3_step(selectstmt) == SQLITE_ROW)
            {
                [content addObject:[NSString stringWithFormat:@"%s",sqlite3_column_text(selectstmt,0)]];
            }
        }
    }

    return content;

}


- (void)viewDidLoad{
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [[documentsDir stringByAppendingPathComponent:@"gamer.sqlite"] copy];

    if(![[NSFileManager defaultManager] fileExistsAtPath:databasePath])
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        int success = [fileManager fileExistsAtPath:databasePath];
        if(success)
        {
            [fileManager removeItemAtPath:databasePath error:nil];
            return;
        }

        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"gamer.sqlite"];

        // Copy the database from the package to the users filesystem
        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];


        documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentsDir = [documentPaths objectAtIndex:0];
    }

 [super viewDidLoad];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];

}

-(IBAction)start_game:(id)sender{
    self.view=frmgame;
    [self view_table];
}
4

1 回答 1

0

不完全是一个直接的答案,但我个人真的很喜欢使用 fmbd 来帮助管理 sqlite 和错误。

Fmdb 是一个围绕 SQLite 的 Objective-C 包装器:http: //sqlite.org/

https://github.com/ccgus/fmdb

于 2013-03-29T23:28:28.507 回答