Detecting If The SQLite Database File Already Exists For iOS App Using NSFileManager Instance | iOS Programmer Guide

To check if the SQLite database file exists for the iOS App, we need to first determine the documents directory for the iOS App. Then by concatenating the documents directory path with the SQLite database file name with extension (db), we can construct the SQLite database file path.

With the help of NSFileManager as given below objective-C code snippet, we can check if the SQLite database already exists as below:


NSString *docsDir;
    NSArray *dirPaths;

    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(
      NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = dirPaths[0];

    // Build the path to the database file
    _databasePath = [[NSString alloc]
       initWithString: [docsDir stringByAppendingPathComponent:
       @"contacts.db"]];

    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: _databasePath ] == NO)
    {
       // Do the db creation process or any other stuffs required
     }