psycopg2 how deal with TypeError: not all arguments converted during string formatting

后端 未结 1 1848
天命终不由人
天命终不由人 2020-12-07 03:18

i have a simple database query by psycopg2 but i do not know why it always show errors here is the code

ip =\"127.0.0.1\"
 sql=\"select count(*) from radacct         


        
相关标签:
1条回答
  • 2020-12-07 04:09

    The sql arguments you pass to execute must be in a tuple or list, even if there's only one of them. This is noted in the documentation:

    For positional variables binding, the second argument must always be a sequence, even if it contains a single variable. And remember that Python requires a comma to create a single element tuple:

    So you need to do it like this:

    ip ="127.0.0.1" 
    sql="select count(*) from radacct where nasipaddress=%s"
    cur.execute(sql, (ip,))
    
    0 讨论(0)
提交回复
热议问题