Python MySQLdb string substitution without added quotations

后端 未结 2 1749
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 03:16

I\'d like to use string substitution for a comma-separated list, such as:

query = \"\"\"SELECT id, name, image_id
           FROM users
           WHERE id IN (%         


        
相关标签:
2条回答
  • 2021-01-21 03:24

    Let MySQLdb do the entire argument substitution. In this case, the expected argument, values, should be the sequence (1,2,3), rather than the string '1,2,3':

    query = """SELECT id, name, image_id
               FROM users
               WHERE id IN %s
               """    
    values = uids
    results = dbc.getAll(query, [values])
    
    0 讨论(0)
  • 2021-01-21 03:44

    For me it worked this way:

    cursor.execute(sql, (pids_list,))
    

    and I made sure that elements of the pids_list are integers and not strings.

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