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 pathexercise5.py
More file actions
101 lines (79 loc) · 2.44 KB
/
exercise5.py
File metadata and controls
101 lines (79 loc) · 2.44 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import psycopg2
import time
from time import sleep
def connect():
connection = None
sleep(0.8)
try:
print('Connect to the PostgreSQL database server...')
connection = psycopg2.connect (
database='PyCoders',
user="postgres",
password="Anahi0615.")
connection.autocommit = True
# CREATE TABLE Students
cursor = connection.cursor()
query = """ CREATE TABLE Students (
student_ID INT NOT NULL,
student_name VARCHAR(120),
CONSTRAINT PK_Students PRIMARY KEY (student_ID)) """
cursor.execute(query)
# CREATE TABLE Teachers
# cursor = connection.cursor()
query = """ CREATE TABLE Teachers (
teacher_ID INT NOT NULL,
teacher_name VARCHAR(120),
CONSTRAINT PK_Teachers PRIMARY KEY (teacher_ID)) """
cursor.execute(query)
# insert tData INTO Students
query = """ INSERT INTO Students
(student_ID, student_name)
VALUES
(
1, 'Shatha Al-Ashwal'
),
( 2, 'Ghassan Alabsi'
),
( 3, 'Juan Obregon'
) """
cursor.execute(query)
# insert Data INTO Teachers
query = """ INSERT INTO Teachers
(teacher_ID, teacher_name)
VALUES
(
1, 'Irfan Karadeniz'
),
( 2, 'Semith'
),
( 3, 'Irem Ugurlu'
) """
cursor.execute(query)
# viewData Student
query = 'Select student_ID, student_name from students'
cursor.execute(query)
sleep(0.8)
print('Students List :')
for fila in cursor:
print(fila)
sleep(0.4)
print()
# viewData Teachers
query = 'Select teacher_ID, teacher_name from Teachers'
cursor.execute(query)
sleep(0.8)
print('Teachers List :')
for fila in cursor:
print(fila)
sleep(0.4)
print()
cursor.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if connection is not None:
connection.close()
sleep(0.8)
print('Database connection closed.')
if __name__ == '__main__':
connect()