I am running this sort of query:
insert into mytable (id, col1, col2)
values (:ID, :COL1, :COL2)
In Python, a dictionary of this form can be used in conjuction with the query above for parameter substitution:
d = { 'ID' : 0, 'COL1' : 'hi', 'COL2' : 'there' }
cursor.execute(sql_insert, d)
But in the real problem, there are lots of columns and lots of rows. Sometimes the data source that populates the dictionary doesn't have an entry. However, if the dictionary is short, Sqlite will complain that an incorrect number of bindings was supplied, rather than giving me a way to add empty strings or NULLs in the columns which are not populated in this case.
I'm being a lazy, or a bit perfectionist. I could write some code to add any missing fields to the dictionary. I'm just looking for an elegant solution that doesn't require triplicating the list of fields.
I tried overloading the dictionary with a modified dictionary that returns an empty string if the field is missing.