Python challenging string encoding

前端 未结 1 1058
说谎
说谎 2021-01-22 02:01

I have the following list of providers (in Russian):

providers = [u\'\\u041e\\u041e\\u041e \"\\u041a\\u0432\\u0430\\u0440\\u0442\\u0430\\u043b 
            \\u04         


        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-22 02:48

    You should not use .format() to include values in a sql query. Use sql parameters instead:

    sql += " WHERE provider IN ({}) GROUP BY date ORDER BY date ASC".format(', '.join(['%s'] * len(providers)))
    
    cursor.execute(sql, providers)
    

    where providers is the original list.

    The idea is to generate a SQL query with the in test using SQL parameter syntax matching the number of providers in your list: WHERE provider in (%s, %s) ... for a two-provider list. Yes, the MySQLdb sql parameter syntax echoes the old-style python formatting syntax, but is not the same thing.

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