1

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")

LibertyMan
  • 47
  • 6

1 Answers1

1

I like to use %

Like:

"SELECT field1, field2 FROM %s WHERE email = '%s'" % ('table', 'johndoe@gmail.com')
Matheus Hernandes
  • 629
  • 1
  • 6
  • 25
  • 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? – LibertyMan Aug 26 '20 at 18:28
  • Check the types you are passing to that string, you can find a guide to % operator here http://www.python-ds.com/python-3-string-operators – Matheus Hernandes Aug 26 '20 at 18:32
  • 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? – LibertyMan Aug 26 '20 at 18:35
  • I have no experience with AS400, but again, check the type of those variables https://stackoverflow.com/questions/402504/how-to-determine-a-python-variables-type – Matheus Hernandes Aug 26 '20 at 18:38