• FMDB中 databaseWithPath 的使用问题


     阅读fmdb的源码文件(下载地址http://github.com/ccgus/fmdb)会发现下面一段注释,里面提到的创建数据库的方法也在很多博客中被引用,但是跑代码的时候发现,文件并不会像文档中所说的那样自动去创建(哪怕是在沙盒目录下的Documents目录下也不能创建成功)

    /** Create a `FMDatabase` object.

     

     An `FMDatabase` is created with a path to a SQLite database file.  This path can be one of these three:

     

     1. A file system path.  The file does not have to exist on disk.  If it does not exist, it is created for you.

     2. An empty string (`@""`).  An empty database is created at a temporary location.  This database is deleted with the `FMDatabase` connection is closed.

     3. `nil`.  An in-memory database is created.  This database will be destroyed with the `FMDatabase` connection is closed.

     

     For example, to create/open a database in your Mac OS X `tmp` folder:

     

        FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];

     

     Or, in iOS, you might open a database in the app's `Documents` directory:

     

        NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

        NSString *dbPath   = [docsPath stringByAppendingPathComponent:@"test.db"];

        FMDatabase *db     = [FMDatabase databaseWithPath:dbPath];

     

     (For more information on temporary and in-memory databases, read the sqlite documentation on the subject: [http://www.sqlite.org/inmemorydb.html](http://www.sqlite.org/inmemorydb.html))

     

     @param inPath Path of database file

     

     @return `FMDatabase` object if successful; `nil` if failure.

     

     */

      

    问题出在哪了呢?一般的思路是:如果文件创建不成功,可能是路径不存在,需要手动创建路径;如果路径存在,那么就可能是创建方法出了问题。

     

    第一种情况添加下面的代码

     

        if (![[NSFileManager defaultManager] fileExistsAtPath:dbPath])
        {
            [[NSFileManager defaultManager] createFileAtPath:dbPath contents:nil attributes:nil];
        }

      

     第二种情况

    找了fmdb的所有代码,没有创建文件或判空路径的操作,于是猜想,也许是注释文档写好后,功能没加(个人瞎想,不必在意)。

  • 相关阅读:
    CSS中的外边距合并问题
    Web性能优化的途径
    HTML5读书笔记——canvas元素(续)
    HTML5读书笔记——canvas元素
    2016/9/8日志
    【每日一醒】【架构师之路】设计文档之惑
    华为是个好公司,但不全是好员工——记初次压力面试的体验
    忐忑的一天,心里还是小兴奋的
    atexit()函数
    年终心结,心绪的总结!
  • 原文地址:https://www.cnblogs.com/PaulpauL/p/6124683.html
Copyright © 2020-2023  润新知