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

发布于 2024-12-12 05:11:23 字数 1041 浏览 0 评论 0原文

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

    -(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 无法检索它!

I am using the following code to read the records from the customers table. For some reason the resultset returned is always empty.

    -(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; 


}

I have checked in the database multiple times and it shows the data but for some reason the FMDB is not able to retrieve it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薯片软お妹 2024-12-19 05:11:23

将数据库日志错误设置为 true,这将显示您登录控制台,您可以在其中找到问题。

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

set db log errors to true, this will show you log in console where you can find the problem.

FMDatabase *db = [FMDatabase databaseWithPath:databasePath];
db.logsErrors = YES;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文