阅读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: [](
@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的所有代码,没有创建文件或判空路径的操作,于是猜想,也许是注释文档写好后,功能没加(个人瞎想,不必在意)。