Ryan pietras
Ryan pietras

Reputation: 39

how to insert values into user inputted column using pyodbc

im trying to insert values into a column that a user has asked to be changed which is called surnamereq and the user change which is called name1. money_spent is the name of the table and first_name is the column that the user is changing the value of.

This is how it should be written in SQL(i think):

INSERT INTO money_spent(first_name) WHERE last_name = surnamereq VALUES(name1)

This is what ive got in python:

cursor.execute("INSERT INTO money_spent(first_name) WHERE last_name = ?, surnamereq VALUES(name1)")

Thanks

Upvotes: 0

Views: 328

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123654

The documentation for the .execute method shows that the form of the method call is

execute(sql, *parameters)

and states that

The optional parameters may be passed as a sequence, as specified by the DB API, or as individual values.

So, you could either do

surnamereq = 'Thompson'
fname = 'Gord'
#
sql = "UPDATE money_spent SET first_name = ? WHERE last_name = ?"
params = (fname, surnamereq)
cursor.execute(sql, params)

or

surnamereq = 'Thompson'
fname = 'Gord'
#
sql = "UPDATE money_spent SET first_name = ? WHERE last_name = ?"
cursor.execute(sql, fname, surnamereq)

Note that the second approach is a pyodbc-specific extension to the DB API.

Upvotes: 1

Related Questions