title: INSERT into SQLite database won't work in Swift
body: <p>I'm calling a method for inserting data for task in table TASK in SQLite file. When I copy generated query to SQLite Free tool or some other tool query works fine and the record is inserted. But in XCode I get output in console: </p>
  
  <pre><code>Record(s) inserted...
  fatal error: unexpectedly found nil while unwrapping an Optional value
  </code></pre>
  
  <p>and application crashes.</p>
  
  <p>The code is below:</p>
  
  <pre><code>    func saveDataToDatabase(dataToSave: NSArray, requestedMethod: String) {
              let documents = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false);
              let fileURL = documents.URLByAppendingPathComponent(DatabaseConstants.databaseName);
              var db: COpaquePointer = nil;
              if sqlite3_open(fileURL.path!, &amp;db) == SQLITE_OK {
                  print("Database opened...");
                  if (dataToSave.count != 0) {
                      for (var i=0; i&lt;dataToSave.count; i++) {
                          var query: String = "";
                          if (requestedMethod == ServiceMethodsConstants.getAllTasksForUserPluginResponse) {
                              query = "INSERT INTO TASK VALUES (null, '\(dataToSave[i].valueForKey("ACTION")!)', '\(dataToSave[i].valueForKey("ACTUAL_ENDING_DATE")!)', '\(dataToSave[i].valueForKey("ACTUAL_STARTING_DATE")!)', '\(dataToSave[i].valueForKey("ASSIGNOR")!)', \(dataToSave[i].valueForKey("ASSIGNOR_USER_ID")!), '\(dataToSave[i].valueForKey("CONTEXT_CLASS")!)', '\(dataToSave[i].valueForKey("CONTEXT_ID")!)', '\(dataToSave[i].valueForKey("CREATION_DATE")!)', '\(dataToSave[i].valueForKey("CREATOR")!)', '\(dataToSave[i].valueForKey("DESCRIPTION")!)', '\(dataToSave[i].valueForKey("ENDING_DATE")!)', '\(dataToSave[i].valueForKey("ESTIMATED_EFFORT")!)', \(dataToSave[i].valueForKey("ID")!), '\(dataToSave[i].valueForKey("NUMBER")!)', '\(dataToSave[i].valueForKey("PRIORITY")!)', '\(dataToSave[i].valueForKey("PROGRESS")!)', '\(dataToSave[i].valueForKey("PROJECT_NAME")!)', '\(dataToSave[i].valueForKey("STARTING_DATE")!)', '\(dataToSave[i].valueForKey("STATUS")!)', '\(dataToSave[i].valueForKey("TASK_NAME")!)', '\(dataToSave[i].valueForKey("TYPE")!)', 0)";
                            print(query);
                          } else if (requestedMethod == ServiceMethodsConstants.getAllProjectsMAResponse) {
                              query = "";
                          }
                          var statement: COpaquePointer = nil;
                          if sqlite3_prepare_v2(db, query, -1, &amp;statement, nil) != SQLITE_OK {
                              let errmsg = String.fromCString(sqlite3_errmsg(db));
                              print("Error preparing insert: \(errmsg)");
                          }
                          if sqlite3_step(statement) != SQLITE_DONE {
                              let errmsg = String.fromCString(sqlite3_errmsg(db));
                              print("Failure inserting record(s): \(errmsg)");
                          } else {
                              print("Record(s) inserted...");
                              if sqlite3_reset(statement) != SQLITE_OK {
                                  let errmsg = String.fromCString(sqlite3_errmsg(db))
                                  print("Error resetting prepared statement: \(errmsg)")
                              }
                          }
                      }
                      if sqlite3_close(db) != SQLITE_OK {
                          print("Database closed...");
                      } else {
                          print("Error closing database");
                      }
                  }
              } else {
                  print("Error opening database...");
              }
          }
  </code></pre>
  
  <p>Can someone please help me fix this? Thanks for your help.</p>