title: Testing sqlite database application on device
body: <p>I am working on a database application that works fine in the simulator (inserts, updates, deletes all function correctly).  My problem starts when attempting to install/run the application on an iPhone.</p>
  
  <p>On the initial run, the program creates an editable version of the database in the documents directory of the application.  My test fails when the database is not found in the application bundle.  The database has been added to the Resources folder in XCode.</p>
  
  <p>The code to check for an existing version of the database (coupled with several NSLog statements), and to create one if one does not already exist is as follows:</p>
  
  <p>-(NSString *) createWriteableDatabase
  {
      BOOL databaseExists;
      NSError *error;</p>
  
  <pre><code>NSFileManager *fileManager = [NSFileManager defaultManager];
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDir = [paths objectAtIndex:0];
  NSString *writeableDB = [documentsDir stringByAppendingPathComponent:@"flashCard.db"];
  
  NSLog(@"writeableDB = '%@'.", writeableDB);
  
  databaseExists = [fileManager fileExistsAtPath:writeableDB];
  
  NSLog(@"databaseExists = %d.", databaseExists);
  
  if (databaseExists)
      return writeableDB;
  
  //--- Writeable version of database does not exist -- copy from bundled software. ---
  
  NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] 
                           stringByAppendingPathComponent:@"flashCard.db"];
  
  NSLog(@"defaultPath = '%@'.", defaultPath);
  
  databaseExists = [fileManager fileExistsAtPath:defaultPath];
  
  NSLog(@"databaseExists = %d.", databaseExists);
  
  databaseExists = [fileManager copyItemAtPath:defaultPath toPath:writeableDB error:&amp;error];
  
  if (!databaseExists)
  {
      NSAssert1(0, @"Failed to create writeable database file: '%@'.", [error localizedDescription]);
      return @"";
  }
  
  return writeableDB;
  </code></pre>
  
  <p>}</p>
  
  <p>The messages displayed in the debugger are as follows:</p>
  
  <p>warning: Unable to read symbols for /XCode 3.2.5/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3 (8J2)/Symbols/System/Library/AccessibilityBundles/AccessibilitySettingsLoader.bundle/AccessibilitySettingsLoader (file not found).
  2011-06-10 21:12:11.130 FlashEm[3768:707] writeableDB = '/var/mobile/Applications/9DBC50B5-2559-4E6A-BD21-B5B56D9DA18A/Documents/flashCard.db'.
  2011-06-10 21:12:11.144 FlashEm[3768:707] databaseExists = 0.
  2011-06-10 21:12:11.149 FlashEm[3768:707] defaultPath = '/var/mobile/Applications/9DBC50B5-2559-4E6A-BD21-B5B56D9DA18A/FlashEm.app/flashCard.db'.
  2011-06-10 21:12:11.154 FlashEm[3768:707] databaseExists = 0.
  2011-06-10 21:12:11.169 FlashEm[3768:707] <strong>* Assertion failure in -[DBAccess createWriteableDatabase], /Users/marathoner1234/Projects/FlashEm/Classes/../DBAccess.m:92
  2011-06-10 21:12:11.201 FlashEm[3768:707] *</strong> Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to create writeable database file: 'The operation couldn’t be completed. No such file or directory'.'</p>
  
  <p>Thanks in advance for any help you can give me, and I apologize if my question text is not formatted properly -- this is my first time asking a question on this site.</p>
  
  <p>(I looked at two other questions where the same problem was occurring, and checked to see if the same issues they had applied to my situation, but they do not.)</p>