-1

Is there someone who knows how I can put an input variable (user input) in a SQLQuery in Python?

 x = str(input("some text"))

sqlQuery = "SELECT * FROM tblname WHERE columname = x "

Thank you very much!

VB1
  • 21
  • 3

1 Answers1

0

you could concatenate the strings, Be aware however, that this leaves you vulnerable for SQL injection.

which would make your example

x = str(input("some text"))
sqlQuery = ("SELECT * FROM tblname WHERE columname = " + x)

or, a more readable version

sqlQuery = "SELECT * FROM tblname WHERE columname = %s" % (x)
Ron
  • 155
  • 12
  • 1
    Really? What about SQL injection?? – apomene May 26 '17 at 09:50
  • you should handle that in the input, the question was how to enter a variable in the query, i agree that you should sanetize your inputs, but that wasn't the question – Ron May 26 '17 at 09:52
  • 1
    I agree with @apomene, you should add a warning, since examples tend to get copy&pasted without thinking about (or even knowing) the implications. Also your example _does_ include the user input... ;-) – Christian König May 26 '17 at 09:53