PyMySQL variables in queries

后端 未结 1 1347
我在风中等你
我在风中等你 2021-01-18 22:49

I would like to have a connection between my python code and a SQL database. I have read several ways to do it , but I am failing to get the results.

conn =          


        
相关标签:
1条回答
  • 2021-01-18 23:35

    You have to pass the parameters inside an iterable - commonly a tuple:

    query = 'SELECT id,sing_name,bir_yr FROM singers_list WHERE bir_yr = %s'
    curs.execute(query, (year, ))
    

    Note that I've also replaced the ? placeholder with %s.

    Also note that the MySQL driver would automatically handle the type conversion between Python and MySQL, would put quotes if necessary and escape the parameters to keep you safe from SQL injection attacks.

    0 讨论(0)
提交回复
热议问题