在python脚本中编码查询sql

时间:2017-11-02 12:03:29

标签: python sql parameters

您好我有问题

我想把参数放在一个sql查询中,但由于编码,它很困难,

params = {'external_id': external_id,
         'source': source,
         'distance': distance,
         'date1': date1,
         'date2': date2,
         'date3': date3,
         'date4': date4,
         'date5': date5,
         'date6': date6,
         'date7': date7, 
         'date8': date8 
         }



sql_pays = """
        SELECT
            adm_country_code,
            adm_region_code,
            adm_city_code
        FROM
            Product
        WHERE
            external_id = {external_id}
                AND
            source = '{source}'
                AND
            product_language = 'fr'

""".format(**params) 

我想获取id的目的地

localisation = executor.query_to_df(sql_pays%params)
series1 = localisation.iloc[0,:]

def get_localisation():        
    if series1["adm_city_code"] == "Paris" or series1["adm_city_code"] == "Lyon"  or series1["adm_city_code"] == "Marseille" :            
        return series1.iloc[2,]       
    elif series1["adm_country_code"] == "Espagne" or series1["adm_country_code"] == "Italie" or series1["adm_country_code"] == "France":            
        return series1.iloc[1,]        
    else:            
        return series1.iloc[0,];

loca = get_localisation()
loca

我得到了这个:

'Tha\xc3\xaflande'

然后我在sql查询中使用loca

    query_vues = """
    SELECT email FROM table WHERE member_language = 'fr'            
    AND event_type IN ('View')          
    AND adm_country_code in  ('{loca}')          
    AND (timestamp>='{date4}'           
    OR (timestamp>='{date5}'           
    AND timestamp<='{date6}'))    
    """.format(**params)

我有什么办法让Thaïlande在当地?

非常感谢

<磷>氮

0 个答案:

没有答案