postgre 执行execute参数为元组

    xiaoxiao2026-04-02  3

    <span style="font-family: Arial, Helvetica, sans-serif;">sql = "select title,content,nid from newslist_v2 where nid in (%s)"</span> conn, cursor = get_postgredb() cursor.execute(sql, [ads_str])

    报错:

        psycopg2.DataError: invalid input syntax for integer: .......LINE 1: ... title,content,nid from newslist_v2 where nid in ('(5663445,..

    2. 

     

    <pre name="code" class="python">sql = "select title,content,nid from newslist_v2 where nid in (%s)" conn, cursor = get_postgredb() cursor.execute(sql, (ads_str))

         或

       

    sql = "select title,content,nid from newslist_v2 where nid in (%s)" conn, cursor = get_postgredb() cursor.execute(sql, ads_str)

    报错:

           TypeError: not all arguments converted during string formatting

    3. ok的方法:

         

    sql = "select title,content,nid from newslist_v2 where nid in {0}" conn, cursor = get_postgredb() cursor.execute(sql.format(ads_str))

         

    转载请注明原文地址: https://ju.6miu.com/read-1308413.html
    最新回复(0)