简体   繁体   中英

how to insert a dictionary with sqlsoup

I am trying to insert a dictionary with sqlsoup. Unfortuntely this doesn't work:

db.blah_table.insert({"blah_field" : "blah_value"})

it gives me an error

TypeError: insert() takes exactly 1 argument (2 given)

the update for sqlsoup works fine. I really don't want to break this up into

db.blah_table.insert(blah_field = "blah_value")

which works as well. help!

In python, you can pass a dictionary as keyword arguments to any function by using the ** argument packing syntax.

If you have a dictionary arguments defined as:

arguments = {'blah_field': 'blah_value'}

You can call db.blah_table.insert with that dictionary as keyword arguments like this:

db.blah_table.insert(**arguments)

Under the hood this is equivalent to:

db.blah_table.insert(blah_field=blah_value)

As an aside, while ** can unpack named arguments from a dictionary, a single * can be used to unpack positional arguments from a list or tuple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM