iOS 6:SQLite db 不起作用



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

我也不明白为什么如果我试图改变名字:"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];
}

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

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

https://github.com/ccgus/fmdb

最新更新