0

我在设备上建立了一个 sqlite3 数据库。当我将它检索到 pc 并尝试通过 Xcode 打开它时,我得到了 result_code 14,无法打开数据库。

我需要打开这个数据库并在它存在于 mac 时从中读取。当它在设备上时,我能够访问它并从中读取。

4

2 回答 2

0

您可以使用SQLite Manager Firefox Plugin来读取数据库,或者使用SQLiteManager

您可以在以下位置找到您的 .sqlite 文件:~/Library/Application\ Support/iPhone\ Simulator/User/Applications/ ...

于 2013-08-21T06:59:49.063 回答
0

试试这个代码

在文档目录中创建数据库...

-(void)createdatabase
{
    NSString *databaseName = @"Card.sqlite";        // Name Of DataBase.

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDir = documentPaths[0];

    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager] ;

    success = [fileManager fileExistsAtPath:databasePath];

    if(success)
        return;

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

打开数据库并访问它的内容..

- (void)getData
{
    sqlite3 *database;

    NSString *databasePath;
    NSString *databaseName;
    NSString *documentsDir;

    NSArray *documentPaths;

    databaseName = @"Card.sqlite";

    documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    documentsDir = documentPaths[0];

    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        const char *sqlStatement = "SELECT * FROM Master";

        sqlite3_stmt *compiledStatement;

        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
        {
            [ID removeAllObjects];
            [Name removeAllObjects];
            [Image removeAllObjects];

            while(sqlite3_step(compiledStatement) == SQLITE_ROW)
            {
                [ID addObject:[NSString stringWithFormat:@"%s",(char *) sqlite3_column_text(compiledStatement, 0)]];
                [Name addObject:[NSString stringWithFormat:@"%s",(char *) sqlite3_column_text(compiledStatement, 1)]];
                [Image addObject:[NSString stringWithFormat:@"%s",(char *) sqlite3_column_text(compiledStatement, 2)]];
            }
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);

    [tblView reloadData];
}
于 2013-08-21T07:11:10.727 回答