python - User Input to mySQL database -
i want simple python program prompt user title , search mysql database. have database constructed can't figure out how query properly.
import mysqldb servername = "localhost"; username = "username"; password = "password"; conn = mysqldb.connect(user=username,host=servername, passwd=password, db=db) cursor = conn.cursor() user_input = raw_input("what want watch?:") query= ("select * videos title %s") cursor.execute(query,user_input)
if, hardcode query "where title '%hardcode%' ", works fine. figure solution pretty simple life of me can't figure out. appreciated! i've tried every variation find online no avail. others tried:
query= ("select * videos title concat('%',%s,'%')") cursor.execute(query,(user_input,)) query= ("select * videos title (search)" "values (%s)", user_input)
... don't work.
errors seem revolve around me passing variable user_input through correctly.
you can create query like:
c.execute("select * videos title %s", ("%" + user_input + "%",))
Comments
Post a Comment