Reputation: 101
Error message 'Execution failed on sql ... expecting string, unicode or buffer object' is returned when trying to pass a Python string variable to a SQL query using cx_Oracle. Any help on this issue is greatly appreciated, thanks!
import pandas as pd
import cx_Oracle as ora
var = 'string'
conn = ora.connect('connection_string')
df = pd.read_sql(("SELECT * FROM table WHERE field LIKE '%s'", (var)), conn)
df.head()
Upvotes: 1
Views: 1105
Reputation: 169524
To avoid the chance of SQL-injection attack you should pass the variable in the params
keyword argument:
df = pdsql.read_sql("""SELECT *
FROM table
WHERE field LIKE %(var)s""", conn, params={'var':'string%',})
Upvotes: 3
Reputation: 359
pd.read_sql("SELECT * FROM table WHERE field LIKE '{}'".format(var), conn)
This should do it. You were trying to pass a tuple to a function instead of string/unicode object.
Upvotes: 1