title: With PHP / sqlite3, how can I add a column if it does not exist in a table?
body: <p>I'm looking for a way to add a column to a table only if it does not exist yet in a PHP script using sqlite3. It looks like SQL alone cannot help me here, for example with something like</p>
  
  <pre><code>ALTER TABLE items ADD COLUMN IF NOT EXISTS new_col INTEGER;
  </code></pre>
  
  <p>Well, I was thinking about hacks like trying to run a query and, if it fails, conclude the column doesn't exist yet :</p>
  
  <pre><code>if ($db-&gt;querySingle('SELECT new_col FROM items') === FALSE) {
      // False means the query failed
      $db-&gt;exec('ALTER TABLE items ADD COLUMN new_col INTEGER');
  }
  
  update_stuff_with_new_column($db);
  </code></pre>
  
  <p>I guess it should work, but what if the query failed for another reason (say, a transaction is running) ?</p>
  
  <p>There are a few other solutions I can think of, but they all look the same : "if the query fails, the column probably does not exist, create it". Is there a better way to do it ?</p>