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")
1 Answer 1
I like to use %
Like:
"SELECT field1, field2 FROM %s WHERE email = '%s'" % ('table', '[email protected]')
answered Aug 26, 2020 at 18:13
Matheus Hernandes
6691 gold badge6 silver badges26 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
LibertyMan
Hi @MatheusHernandes I entered my code like this: length=c1.execute("select dtcdl, dtcnum from %s where dtsy=%s and dtrt=%s" % (librTable, product_code, userCode)) Now I get the error of Column or global variable S1 not found. Any idea on this fix?
Matheus Hernandes
Check the types you are passing to that string, you can find a guide to % operator here python-ds.com/python-3-string-operators
LibertyMan
That S1 is a value of the field in the AS400. When I hardcode it it works fine. What recommendation do you have for this?
Matheus Hernandes
I have no experience with AS400, but again, check the type of those variables stackoverflow.com/questions/402504/…
default