FMDB 未插入数据库

Posted

技术标签:

【中文标题】FMDB 未插入数据库【英文标题】:FMDB not inserting into DB 【发布时间】:2011-08-13 11:16:23 【问题描述】:

我正在尝试使用 FMDB 将一些查询插入数据库。 当我检查插入查询时,它说它正在插入。 后来我实际上可以从表格中读取数据。 但是发生的情况是,如果我尝试从数据库中读取信息,它会返回为空。 当我在 Xcode 项目中检查数据库时,似乎没有更新。 这让我有点困惑。任何建议可能是什么问题?

【问题讨论】:

您是否使用db.traceExecution = YESdb.logsErrors 来检查发生了什么? 刚刚检查了 db.traceExecution 和 db.logsErrors。不显示错误并显示交易。 你能发布你用来从数据库读取的代码吗? NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *错误; NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mydatabase.db"];数据库 = [FMDatabase databaseWithPath:defaultDBPath]; FMResultSet *rs = [数据库执行查询:@"SELECT * FROM myList"]; if([数据库打开]) while ([rs next]) NSLog(@"%@ %@ %@ %@ %@", [rs stringForColumn:@"my_id"], [rs stringForColumn:@"my_name" ], [rs stringForColumn:@"my_loc"]); [rs 关闭]; [数据库关闭]; 我注意到数据库位于 iPhone 模拟器中的位置。它没有桌子。 【参考方案1】:

您的代码错误。这是我用来写的:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];

FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
[db open];

[db executeUpdate:@"INSERT INTO foo (bar) VALUES (?)", bar];

[db close];

阅读:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];

    FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
    [db open];

    NSMutableArray *bar = [[[NSMutableArray alloc] init] autorelease];

    FMResultSet *s = [db executeQuery:@"select * from foo"];
    while ([s next]) 

        [bar addObject:[s stringForColumn:@"bar"]];

    

    NSLog(@"%@", bar);

    [db close];

还可以查看我所在数据库的位置。我认为按照你的方式在模拟器上可以工作,但在设备上不行,因为沙盒等等。

祝你好运!

编辑:

把它放在你的应用委托中:

- (void)createEditableCopyOfDatabaseIfNeeded 


    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) 
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    

然后从application:didFinishLaunchingWithOptions 运行该方法:

[self performSelector:@selector(createEditableCopyOfDatabaseIfNeeded) withObject:nil];

确保首先将数据库放入项目树中。

【讨论】:

谢谢,我会试一试。 是的。它没有插入数据库。它可能是我的数据库的路径吗?我有它在根文件夹中。

以上是关于FMDB 未插入数据库的主要内容,如果未能解决你的问题,请参考以下文章

FMDB 数据库和 xcode 插入数据库

带有 FMDB 包装器的 Sqlite 数据库不插入数据

使用 FMDB 插入表失败

FMDB:添加新列并插入数据

FMDB批量插入数据

使用 isTransaction 在 FMDB 数据库中插入三千个字符串数据