-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstarter_code_part2.py
More file actions
41 lines (27 loc) · 1.38 KB
/
starter_code_part2.py
File metadata and controls
41 lines (27 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Import Library
import psycopg2
from psycopg2 import Error
# Complete the function to create connection
def getConnection():
# Write Your Code Here
def closeConnection(connection):
connection.close()
print("Connection is Closed")
def getCustomer(customer_id):
try:
connection = getConnection() # Create a connection using the connection function
# Write Your Code Here - Create a cursor object from the connection
# Write Your Code Here - Create a query variable, by writing a SELECT query to fetch the customer using the customer_id as the filter.
cursor.execute(query, (customer_id,))
# Write Your Code Here - Create an object that will hold the records, Hint use fetchall()
print("Printing Records")
"""Write Your Code Here - Using a for loop, iterate through the records to print each record instance of the customer table.
Example of the print statement - print("Customer ID", row[0])
Check the table in your pg admin to know all the records you will print out.
For Example - print("Address ID", row[5]) is a record instance of the address_id column
""""
closeConnection(connection) # Closes the connection
except Error as e:
print("Error: Reading Record")
# Using the getCustomer function, print out the customer information for customer_id = 34
getCustomer()