iOS的SQLite利用FMDB进行操作时无法打开数据库


1.按照FMDB文档格式进行SQLite的数据库建立,以及建表、插入、关闭的操作。但是真机测试时似乎是权限问题,不让在手机的documentation目录下进行读写操作。

2.源代码如下:


 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];

    NSString *infraredDataBasePath = [documentDirectory stringByAppendingPathComponent:@"ifraredDataBase.sqlite"];
    //建立ifraredDataBase
    FMDatabase *ifraredDataBase = [FMDatabase databaseWithPath:infraredDataBasePath];

    BOOL res1111 = [ifraredDataBase open];
    if (res1111 == YES)
    {
        NSLog(@"ifraredDataBase 打开成功!");

        //建表
        [ifraredDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS ifraredTable (temp REAL, date TEXT)"];
        //插入数据
        NSString *insertTempAndDate = [NSString stringWithFormat:
                                       @"INSERT INTO ifraredTable (temp, date) VALUES ('%@', '%@')",
                                       tempForDB, timeForDB];
        BOOL res = [ifraredDataBase executeUpdate:insertTempAndDate];

        if (res == YES)
        {
            NSLog(@"SQLite插入数据成功!");
        }
        else
        {
            NSLog(@"SQLite插入数据失败!");
        }
    }
    else
    {
        NSLog(@"ifraredDataBase 打开失败!");
    }
    [ifraredDataBase close];

3.Xcode反馈的错误信息如下:


 2015-09-07 21:36:24.833 iWorkaides[1242:156531] error opening!: 14
2015-09-07 21:36:24.833 iWorkaides[1242:156531] ifraredDataBase 打开失败!
Printing description of infraredDataBasePath:
/var/mobile/Containers/Data/Application/C13AB1F7-B11C-4E4F-887B-A8CD76E16599/Library/Documentation/ifraredDataBase.sqlite
Printing description of infraredDataBasePath:
/var/mobile/Containers/Data/Application/C13AB1F7-B11C-4E4F-887B-A8CD76E16599/Library/Documentation/ifraredDataBase.sqlite
Printing description of ifraredDataBase:
<FMDatabase: 0x170669dc0>

4.找了好久文档仍旧无法解决,着急,希望能有人能帮帮我。 :)

ios fmdb sqlite

影子dē逆袭 9 years, 4 months ago

因为你的是空表吧。


 if (![ifraredDataBase open]) {
        NSLog(@"OPEN FAIL");
}

[ifraredDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS MyTable(aa float,bb text,cc integer,dd integer,ee text)"];
[ifraredDataBase close];

偶是loli answered 9 years, 4 months ago

模拟器可以吗?

Fwcke answered 9 years, 4 months ago

一般不是写在Documents文件夹下么
刚才试了一下,如果创建数据库时,路径的NSdocumentDirectory改成NSDocumentationDirectory,打印路径后复制路径查找时,会提示查不到文件夹,程序的错误提示跟楼主的一致。所以,建议楼主将路径改回Documents文件夹下

エロ階級は曹長 answered 9 years, 4 months ago

Your Answer