sql—在python中基于if条件从红移数据库返回结果

zphenhs4  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(266)

我需要从一个 redshift database 基于 IF 用python编写的条件。
假设我有一张table CustomerID, SHipment Number, Invoice Station, ect 作为中的列 Redshift table ,我想从redshift表中获取所有记录,如果客户id存在,则应使用用户输入进行检查。
表名=shipmentinfo
列=customerid、billnumber、开票站、费用等。
python

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)
vbkedwbf

vbkedwbf1#

考虑用户输入值的参数化(否则就要冒臭名昭著的bobby表的风险)。


# PREPARED STATEMENT WITH PLACEHOLDER

sql = """SELECT source_system, file_nbr, file_date, CustomerID 
         FROM public.shipment_info 
         WHERE CustomerID = %s
      """

# BIND PARAM VALUES WITH TUPLE OF ONE-ITEM

cur.execute(sql, (HWB,))

相关问题