0
exp = "Ted is a good film"
cursor.execute ("insert into films (descp) values (exp)")
cursor.commit()

I'm using above code with MS SQL server, but it says: Invalid column name'exp' I'm using pyodbc.

2
  • 2
    FWIW, any of the solutions would work, but (at present) xbb's is the only one which isn't potentially vulnerable to SQL injection. Commented Apr 22, 2013 at 15:44
  • @Aya why you think that I'm using this for sql injection ? Commented Apr 22, 2013 at 15:54

3 Answers 3

10
cursor.execute ("insert into films (descp) values (?)",exp)
Sign up to request clarification or add additional context in comments.

2 Comments

My solution works, but xbb is more correct solution. Documentation
Your solution is worked, but it says : The statement has been terminated
3

I think you should pass it as a tuple:

cursor.execute ("insert into films (descp) values (?)", (exp,))

Comments

1

You need introduce exp content into insert expression as string. You can use string format and ' ':

exp = "Ted is a good film"
cursor.execute ("insert into films (descp) values ('{exp}')".format(exp=exp))
cursor.commit()

2 Comments

After trying it said : The statement has been terminated.
How about exp = "anything'); drop table films; --" ?

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.