lekso borashvili
lekso borashvili

Reputation: 65

Python how to pass multiple arguments as a list instead of passing them separately

I am trying to insert into an sql database and have 20 different arguments that I need to pass as '?'. I have all these parameters in a list and am wondering if there is a way or a shortcut that can automate passing these arguments instead of writing all of them down?

Upvotes: 0

Views: 416

Answers (1)

Stephen
Stephen

Reputation: 1518

PEP249 (the Python DB API 2.0 Specification) already defines that passing parameters to the execute method already allows a sequence or mapping:

Parameters may be provided as sequence or mapping and will be bound to variables in the operation. Variables are specified in a database-specific notation

Because this is the case, you can take your list my_list and simply pass it as is to the execute method:

connection = my_db_driver.connect('connection_string')
my_cursor = connection.cursor()
my_parameter_list = ['lesko borashvili', 122121211]
my_cursor.execute('SELECT * FROM MY_TABLE WHERE my_user = ? and my_order = ?', my_parameter_list)

Upvotes: 1

Related Questions