0

Why this code returns nothing?

ip = '10.113.205.55'

cursor.execute("Select * from tablename WHERE ip like '%s' "), ip

result = cursor.fetchall()

print result

when i use the code below it works:

cursor.execute("Select * from tablename where ip like '10.113.205.55' ")

result = cursor.fetchall()

print result
Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58
  • Possible duplicate of [How to use variables in SQL statement in Python?](http://stackoverflow.com/questions/902408/how-to-use-variables-in-sql-statement-in-python) – midori Jan 28 '16 at 17:39

2 Answers2

1

You should remove the ' and put ip inside the parameter list:

cursor.execute("SELECT * FROM tablename WHERE ip LIKE %s", [ip])
Daniel
  • 42,087
  • 4
  • 55
  • 81
0

change the argument of cursor.execute method to:cursor.execute("Select * from tablename WHERE ip like '%s'" % ip)

Ismael Infante
  • 522
  • 4
  • 8