title: SQL connection error from PHP after many operations
body: <p>I'm currently looping to create a MBTiles map, and add information to my database each time.
  Here's how I configured my connection and execute actions during the loop:</p>
  
  <pre><code>if ($pdo_mbtiles == null) {
      echo "Opening new database connection".PHP_EOL;
      $pdo_mbtiles = new PDO('sqlite:'.$filename,
              '',
              '',
              array(
                  PDO::ATTR_PERSISTENT =&gt; true
                  )
              );
      $pdo_mbtiles-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
      $pdo_mbtiles-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  }
  
  
  $q = $pdo_mbtiles-&gt;prepare("INSERT INTO tiles (zoom_level, tile_column, tile_row,tile_data) VALUES (:zoom_level, :tile_column, :tile_rowTMS, :tile_data)");
  $q-&gt;bindParam(':zoom_level', $zoom_level);
  $q-&gt;bindParam(':tile_column', $tile_column);
  $q-&gt;bindParam(':tile_rowTMS', $tile_rowTMS);
  $q-&gt;bindParam(':tile_data', $tile_data, PDO::PARAM_LOB);
  $q-&gt;execute();
  </code></pre>
  
  <p>After 1018 times looping (this number doesn't change no matter how many times I try), I get this error message:</p>
  
  <pre><code>SQLSTATE[HY000]: General error: 14 unable to open database file
  </code></pre>
  
  <p>I checked the solution written here:
  <a href="https://stackoverflow.com/questions/25668156/how-to-prevent-sqlite-sqlstatehy000-14">How to prevent SQLITE SQLSTATE[HY000] [14]?</a>
  but the echoed message only appears at the first time of the loop, so I assume the PDO connection isn't closed.</p>
  
  <p>I didn't find other documentation relevant to this error code.</p>
  
  <p>What could possibly go wrong here?</p>
  
  <p>I tried to move the prepare and bind commands in a condition as follows. The exception isn't raised, but only the first tile is saved (or every tile is saved on top of the first one, not sure):</p>
  
  <pre><code>if ($pdo_mbtiles == null) {
      echo "Opening new database connection".PHP_EOL;
      $pdo_mbtiles = new PDO('sqlite:'.$filename,
              '',
              '',
              array(
                  PDO::ATTR_PERSISTENT =&gt; true
                  )
              );
      $pdo_mbtiles-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
      $pdo_mbtiles-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  }
  
  if ($q == null) {
      $q = $pdo_mbtiles-&gt;prepare("INSERT INTO tiles (zoom_level, tile_column, tile_row,tile_data) VALUES (:zoom_level, :tile_column, :tile_rowTMS, :tile_data)");
      $q-&gt;bindParam(':zoom_level', $zoom_level);
      $q-&gt;bindParam(':tile_column', $tile_column);
      $q-&gt;bindParam(':tile_rowTMS', $tile_rowTMS);
      $q-&gt;bindParam(':tile_data', $tile_data, PDO::PARAM_LOB);
  }
  $q-&gt;execute();
  </code></pre>
  
  <p>Here are the files during the generation:
  <a href="https://i.stack.imgur.com/OFxxI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OFxxI.png" alt="files_during_process"></a></p>
  
  <p>And here after the exception is raised:
  <a href="https://i.stack.imgur.com/SpPlk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SpPlk.png" alt="files_after_process"></a></p>
  
  <p>Also, when the exception is raised, I do a var_dump of my $pdo_mbtiles, and here's the result (exactly the same as when I do it with a success):</p>
  
  <pre><code>object(PDO)#116 (0) {
  }
  </code></pre>
  
  <p><strong>Edit:</strong> Still trying to solve this problem, I simplified the code to create the MBTiles file. No success yet, but here's a sample if anyone want's to reproduce the issue. You can download it from <a href="https://www.dropbox.com/s/33vqamc9tn4c3ux/sample_PHP_MBTiles_generation_bug.zip?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/33vqamc9tn4c3ux/sample_PHP_MBTiles_generation_bug.zip?dl=0</a></p>