forked from fenyx-it-academy/Class7-Database-Module-Week8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise4.py
More file actions
69 lines (55 loc) · 1.89 KB
/
exercise4.py
File metadata and controls
69 lines (55 loc) · 1.89 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import psycopg2
from time import sleep
def connect():
connection = None
sleep(0.5)
try:
print('Connect to the PostgreSQL database server...')
connection = psycopg2.connect (
database='pagila-data',
user="postgres",
password="Anahi0615.")
connection.autocommit = True
# All rows from actor table fetchall
print("+-------------------------------+")
print("| All rows from actor table |")
print("+-------------------------------+")
cursor = connection.cursor()
query = """ SELECT * FROM actor """
cursor.execute(query)
result = cursor.fetchall()
for r in result:
print(r)
sleep(0.069)
# First row of category table with fetchone
print()
print("+-------------------------------+")
print("| First row of category table |")
print("+-------------------------------+")
cursor = connection.cursor()
query = """ SELECT * FROM category """
cursor.execute(query)
result = cursor.fetchone()
print(result,"\n")
sleep(0.3)
# 50 rows of address table fetchmany
print("+-------------------------------+")
print("| 50 rows of address table |")
print("+-------------------------------+")
cursor = connection.cursor()
query = """ SELECT * FROM address """
cursor.execute(query)
result = cursor.fetchmany(size=50)
for r in result:
print(r)
sleep(0.069)
cursor.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if connection is not None:
connection.close()
sleep(0.3)
print('\nDatabase connection closed.\n')
if __name__ == '__main__':
connect()