【问题标题】:NSDocumentDirectory giving strange directory as outputNSDocumentDirectory 给出奇怪的目录作为输出
【发布时间】:2012-11-05 17:11:02
【问题描述】:

我正在尝试从我的文档目录中打开 sqlite 数据库:

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dbFileName = [docDir stringByAppendingPathComponent:@"DatabaseName.sqlite"];

self.db = [FMDatabase databaseWithPath:dbFileName];

self.db.logsErrors = YES;

self.db = _db;
if ([self.db open]) {
    NSLog(@"database opened");

}
NSLog(@"docDir = %@",[NSString stringWithFormat:@"%@%@",docDir,dbFileName]);

NSLog 显示奇怪的路径 docDir = /Users/userName/Documents/Users/userName/Documents/DatabaseName.sqlite 而不是 /Users/userName/Documents/DatabaseName.sqlite。打开时没有错误或警告。

在此之后,我尝试从我的表中获取 count(*)

NSString *queryString = [NSString stringWithFormat:@"SELECT count(*) FROM histories"];
FMResultSet *result = [self.db executeQuery:queryString];
NSLog(@"count = %i", [result intForColumnIndex:0]);

当 10k 行但 NSLog 显示为 0 时,数据库有更多。应用程序不适用于 iOS,仅适用于命令行。我在哪里可以找到问题?

【问题讨论】:

    标签: objective-c sqlite fmdb


    【解决方案1】:

    你有

    docDir = /Users/userName/Documents
    dbFileName = /Users/userName/Documents/DatabaseName.sqlite
    

    所以在

    NSLog(@"docDir = %@",[NSString stringWithFormat:@"%@%@",docDir,dbFileName]);
    

    docDir 被打印两次(dbFileName 已经包含 docDir)。

    备注:self.db = _db; 的声明看起来很可疑,您可能需要删除它。

    添加: FMDB 文档指出:

    在尝试访问之前,您必须始终调用 -[FMResultSet next] 查询中返回的值,即使您只期望一个。

    所以你的代码应该是这样的:

    FMResultSet *result = [self.db executeQuery:queryString];
    if ([result next]) {
        NSLog(@"count = %i", [result intForColumnIndex:0]);
    }
    

    【讨论】:

    • @RomanHouse:我不知道(-:-你删除了self.db = _db;吗?
    • 是的,删除了这一行,但仍然是 0。
    猜你喜欢
    • 2014-01-21
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多