Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 878547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:47:20+00:00 2026-05-15T11:47:20+00:00

I am running this sort of query: insert into mytable (id, col1, col2) values

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:47:20+00:00Added an answer on May 15, 2026 at 11:47 am

    I haven’t checked that this works, but I think it should:

    from collections import defaultdict
    d = { 'ID' : 0, 'COL1' : 'hi' }
    cursor.execute(sql_insert, defaultdict(str, d))
    

    defaultdict is a specialised dictionary where any missing keys generate a new value instead of throwing a KeyError.

    Of course this only works if all the values need the same default such as an empty string or None. If you need different defaults then you’ll need a dictionary containing the defaults and you can do:

    DEFAULTS = { ... whatever ... }
    d = { 'ID' : 0, 'COL1' : 'hi' }
    cursor.execute(sql_insert, dict(DEFAULTS).update(d))
    

    Note that you must copy DEFAULTS each time so you can update the copy with the actual values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running into this issue of releasing an already released object but can't
How would you describe the running time of this sort, given a function sorted
Been running into this problem lately... When debugging an app in VS.Net 2005, breakpoints
I've been running into this problem with Flex for nearly a year, and each
I am running this query SELECT sh.*, u.initials AS initals FROM database1.table1 AS sh
I've got a bunch of servers running this Linux app. I'd like for them
I'm in javascript, running this in the console d = new Date(); d.setMonth(1); d.setFullYear(2009);
I keep running across this loading image http://georgia.ubuntuforums.com/images/misc/lightbox_progress.gif which seems to have entered into
I got this output when running sudo cpan Scalar::Util::Numeric jmm@freekbox:~/bfwsandbox/sa/angel/astroportal/dtu8e/resources$ sudo cpan Scalar::Util::Numeric [sudo]
Running a ServiceHost with a single contract is working fine like this: servicehost =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.