title: python sqlite insert named parameters or null
body: <p>I'm trying to insert data from a dictionary into a database using named parameters. I have this working with a simple SQL statement e.g.</p>
  
  <pre><code>SQL = "INSERT INTO status (location, arrival, departure) VALUES (:location, :arrival,:departure)"
  dict = {'location': 'somewhere', 'arrival': '1000', 'departure': '1001'}
  c.execute(SQL,dict)
  </code></pre>
  
  <p>Inserts somewhere into location, 1000 into the arrival column, and 1001 into departure column.</p>
  
  <p>The data that I will actually have will contain location but may contain either arrival, or departure but might not have both (in which case either nothing or NULL can go into the table). In this case, I get sqlite3.ProgrammingError: You did not supply a value for binding 2.</p>
  
  <p>I can fix this by using defaultdict:</p>
  
  <pre><code>c.execute(SQL,defaultdict(str,dict))
  </code></pre>
  
  <p>To make things slightly more complicated, I will actually have a list of dictionaries containing multiple locations with either an arrival or departure.</p>
  
  <pre><code>    ({'location': 'place1', 'departure': '1000'},
      {'location': 'palce2', 'arrival': '1010'},
      {'location': 'place2', 'departure': '1001'})
  </code></pre>
  
  <p>and I want to be able to run this with c.executemany however I now can't use defaultdict.</p>
  
  <p>I could loop through each dictionary in the list and run many c.execute statements, but executemany seems a tidier way to do it.</p>
  
  <p>I've simplified this example for convenience, the actual data has many more entries in the dictionary, and I build it from a JSON text file.</p>
  
  <p>Anyone have any suggestions for how I could do this?</p>