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'm running a query on a table that contains values, some of the form
I am running into this Postgres issue where the same query takes a long
Im running this query on the same server as the web application, so SPQuery.ExpandRecurrence
I'm running this query: SELECT TOP 1 [DVD Copy].[Stock No] FROM [DVD Copy] WHERE
I am running this query SELECT sh.*, u.initials AS initals FROM database1.table1 AS sh
This is a simple sort of crosstab query. I am having difficulty transferring my
I am attempting to query time data from MySQL in PHP, using this sort
Try running this in a .VBS file MsgBox(545.14-544.94) You get a neat little answer
In running this on index.html, I get the following error: Uncaught SyntaxError: Unexpected token
When running this powershell command Get-ChildItem -Recurse -Include *.txt You get multiple tables in

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.