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

1 Answer 1

1

I like to use %

Like:

"SELECT field1, field2 FROM %s WHERE email = '%s'" % ('table', '[email protected]')
Sign up to request clarification or add additional context in comments.

4 Comments

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?
Check the types you are passing to that string, you can find a guide to % operator here python-ds.com/python-3-string-operators
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?
I have no experience with AS400, but again, check the type of those variables stackoverflow.com/questions/402504/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.