根据Python中的IF条件从红移数据库返回结果

根据Python中的IF条件从红移数据库返回结果,python,sql,postgresql,amazon-redshift,Python,Sql,Postgresql,Amazon Redshift,我需要根据用Python编写的IF条件从红移数据库中提取结果 假设我有一个表,其中CustomerID、装运编号、发票站等列在红移表中,如果存在客户ID,我想从红移表中获取所有记录,这应该通过用户输入进行检查 表名=ShipmentInfo 列=客户ID、账单号、开票站、费用等。 蟒蛇 import psycopg2 con=psycopg2.connect(dbname= 'datamodel', host='123', port= '5439',

我需要根据用Python编写的IF条件从红移数据库中提取结果

假设我有一个表,其中CustomerID、装运编号、发票站等列在红移表中,如果存在客户ID,我想从红移表中获取所有记录,这应该通过用户输入进行检查

表名=ShipmentInfo 列=客户ID、账单号、开票站、费用等。 蟒蛇

import psycopg2

con=psycopg2.connect(dbname= 'datamodel', host='123', 
                     port= '5439', user= 'bce', password= 'Ciz')

cur = con.cursor()
HWB = input("Enter the House Bill Number : ")

#if CustomerID = HWB:
cur.execute("SELECT source_system, file_nbr, file_date, CustomerID 
             FROM public.shipment_info where CustomerID = $HWB")

results = cur.fetchall()

cur.close() 
con.close()

print(results)

考虑用户输入值的参数化,否则将冒着臭名昭著的风险

带占位符的准备语句 sql=选择源系统、文件编号、文件日期、客户ID 从public.shipping\u info 其中CustomerID=%s 用一项元组绑定参数值 cur.executesql,HWB,