0

I have a variable like below in python;

run_id=5654

When i execute the below code;

df=curs.execute("select* from [DATABASE] where RunId=run_id")

I got an error:

DataError: ('22018', "[22018] [Microsoft][ODBC SQL Server Driver][SQL Server]Conversion failed when converting the varchar value 'run_id' to data type smallint. (245) (SQLExecDirectW)")

Could you please help me about this? How can i proceed?

2

1 Answer 1

1

Assuming you are connecting to your SQL Server instance using pyodbc, you should use a prepared statement with ? as the placeholder:

run_id = '5654'
df = curs.execute("SELECT * FROM [DATABASE] WHERE RunId = ?", (run_id,))
Sign up to request clarification or add additional context in comments.

2 Comments

yeah you are right, but i got this error: ProgrammingError: ('Invalid parameter type. param-index=0 param-type=numpy.int64', 'HY105')
It appears that the RunId column is some sort of text. Then you need to bind a string variable, q.v. my updated answer.

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.