TypeError: 'int' object is not iterable - Python

前端 未结 2 552
忘掉有多难
忘掉有多难 2021-02-09 04:01

I got the following error:

  File \"/home/ec2-user/test/test_stats.py\", line 43, in get_test_ids_for_id
    cursor.execute(\"\"\"select test_id from test_logs w         


        
2条回答
  •  名媛妹妹
    2021-02-09 04:58

    You need to give cursor.execute a tuple, but you only gave it one integer:

    (id)
    

    Add a comma to make that a tuple:

    (id,)
    

    The full line then'd be:

    cursor.execute("""select test_id from test_logs where id = %s """, (id,))
    

    Putting an expressione in parentheses just 'groups' that one expression. It is the comma that makes something a tuple:

    >>> (42)
    42
    >>> (42,)
    (42,)
    

    Any iterable will do really, so you could also use [...] brackets:

    cursor.execute("""select test_id from test_logs where id = %s """, [id])
    

提交回复
热议问题