0

I need to extract results from a redshift database based on an IF condition written in Python.

Suppose I have a table with CustomerID, SHipment Number, Invoice Station, ect as columns in Redshift table, I want to get all the records from Redshift table if customer ID exists which should be checked with user input.

  • TABLE NAME = ShipmentInfo
  • COLUMNS = CustomerID, BillNumber, Invoicing Station, Charges, etc.

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)

1 Answer 1

1

Consider parameterization of user input value (else risk the infamous, Bobby Tables).

# 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,))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.