FMDB从数据库中读取iPhone应用程序

时间:2011-10-24 18:07:10

标签: objective-c ios fmdb

我使用以下代码来读取customers表中的记录。由于某种原因,返回的结果集始终为空。

    -(NSMutableArray *) getCustomers
{
    NSMutableArray *customers = [[NSMutableArray alloc] init];

    NSString *databaseName = @"Customers.db";

    NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentsPath objectAtIndex:0];

    NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    FMDatabase *db = [FMDatabase databaseWithPath:databasePath];

    if(![db open]) 
    {
        return nil; 
    }

    FMResultSet *results = [db executeQuery:@"SELECT * FROM customers"];

    while([results next]) 
    {
        Customer *customer = [[Customer alloc] init];


        customer.firstName = [results stringForColumn:@"firstname"];
        customer.lastName = [results stringForColumn:@"lastname"];

        [customers addObject:customer];

    }

    [db close];

    return customers; 


}

我已多次检查数据库并显示数据,但由于某种原因,FMDB无法检索它!

1 个答案:

答案 0 :(得分:2)

将db log errors设置为true,这将显示登录控制台,您可以在其中找到问题。

FMDatabase *db = [FMDatabase databaseWithPath:databasePath];
db.logsErrors = YES;