LibertyMan
LibertyMan

Reputation: 47

How to pass in parameters to a SQL statement in Python

I have a Python script that is calling a function that executes a SQL select statement but I keep getting errors that the Token ? was not valid. How do I pass in variables so that they work with the SQL statement? Here is my code:

def get_jde_udc_info(connection,product_code,userCode,librTable):

    c1=connection.cursor()
    
    length=c1.execute("select dtcdl, dtcnum from ?",(librTable) + " where dtsy=?",(product_code) + " and dtrt=?",(userCode))

    length=c1.fetchall() # <-- Using the .fetchone method from the Python cursor class that will return a single record or None if no more rows are available
    
    print(length)

Here is the function call in my script:

get_jde_udc_info(connection,"41","s1","testctl.f0004")

Upvotes: 1

Views: 943

Answers (1)

Matheus Hernandes
Matheus Hernandes

Reputation: 659

I like to use %

Like:

"SELECT field1, field2 FROM %s WHERE email = '%s'" % ('table', '[email protected]')

Upvotes: 1

Related Questions