iOS 6:SQLite数据库不起作用

时间:2013-03-29 21:33:07

标签: iphone ios sqlite

我总是使用此代码在我的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];
}

1 个答案:

答案 0 :(得分:0)

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

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

https://github.com/ccgus/fmdb