3 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
可能不合時(shí)宜,但“Y”會(huì)拉斷你的琴弦嗎?
query = 'SELECT name,billto_only, controlling_only , load_create_date, load_update_date, nbr, owner, sales_rep, source_system, state, station, zip FROM public.test WHERE billto_only = \'Y\' OR controlling_only = \'Y\' AND load_create_date >= \''+time+'\' OR load_update_date >= \''+time+'\''
轉(zhuǎn)義撇號(hào)

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊
執(zhí)行此操作的正確方法:
query = """SELECT
name, account_mgr, addr1, addr2, ap_email, bill_to_nbr, billto_only, city, controlling_nbr,
controlling_only, country, cust_contact, email, load_create_date, load_update_date, nbr,
owner, sales_rep, source_system, state, station, zip
FROM
public.customers
WHERE
billto_only = 'Y' OR controlling_only = 'Y'
AND
load_create_date >= %(time_val)s OR load_update_date >= %(time_val)s
"""
queryData = execute_db_query(query, {"time_val": time)
以上確保了傳入的數(shù)據(jù)被正確引用并防止 SQL 注入問(wèn)題。我也會(huì)避免使用像time. time這是與模塊以及timePostgres 中的類型的潛在沖突。

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
發(fā)生錯(cuò)誤的原因是'
符號(hào),
"SELECT name, account_mgr, addr1, addr2, ap_email, bill_to_nbr, billto_only, city, controlling_nbr, controlling_only, country, cust_contact, email, load_create_date, load_update_date, nbr, owner, sales_rep, source_system, state, station, zip FROM public.customers WHERE billto_only = 'Y' OR controlling_only = 'Y' AND load_create_date >= '{date}' OR load_update_date >= '{date}'".format(date=time)
添加回答
舉報(bào)