简体   繁体   中英

Python oursql treating a string variable as a char array

Forgive my ignorance as I am new to oursql. I'm simply trying to pass a parameter to a statement:

cursor.execute("select blah from blah_table where blah_field = ?", blah_variable)

this treated whatever is inside the blah_variable as a char array so if I pass "hello" it will throw a ProgrammingError telling me that 1 parameter was expected but 5 was given.

I've tried looking through the docs but their examples are not using variables. Thanks!

IT is expecting a sequence of parameters. Use:

[blah_variable]

The cursor.execute() call expects a params argument that is iterable. The documentation only hints at it , but the code actually unpacks the argument and passes it to another function:

# from oursqlx/cursor.pyx:121
# in Cursor.execute()
        else:
            stmt.execute(*params)

You would need to phrase your call like:

cursor.execute("select blah from blah_table where blah_field = ?", [blah_variable]) # brackets!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM