-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAn Example for Filtering Operation in PythonGroup A using a PostgreSQL Database.py
54 lines (47 loc) · 1.66 KB
/
An Example for Filtering Operation in PythonGroup A using a PostgreSQL Database.py
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
import psycopg2
# Database connection parameters
host = "localhost" # or the appropriate hostname if not local
database = "TwitterProject"
user = "postgres"
password = "your password"
# Connection string
conn_string = f"host={host} dbname={database} user={user} password={password}"
# Connect to the PostgreSQL database
try:
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
print("Connected to the database.")
# Create table Python_Group_A
cursor.execute("""
CREATE TABLE IF NOT EXISTS Python_Group_A (
ID CHAR(4) PRIMARY KEY,
Firstname VARCHAR(255),
Surname VARCHAR(255),
Position VARCHAR(255),
Gender CHAR(6),
RegistrationDate DATE,
Status VARCHAR(10)
);
""")
print("Table 'Python_Group_A' created successfully.")
# Insert data into Python_Group_A table
cursor.execute("""
INSERT INTO Python_Group_A (ID, Firstname, Surname, Position, Gender, RegistrationDate, Status)
VALUES
('0001', 'Farshid', 'Keivanian', 'Teacher', 'Male', '2024-05-04', 'Online'),
('0002', 'Sanaz', 'Jafari', 'Student', 'Female', '2024-05-04', 'Online')
ON CONFLICT (ID) DO NOTHING;
""")
conn.commit()
print("Data inserted into 'Python_Group_A' table successfully.")
# Query to filter and display data based on gender
cursor.execute("SELECT * FROM Python_Group_A WHERE Gender = 'Female';")
rows = cursor.fetchall()
for row in rows:
print(row)
except Exception as e:
print(f"An error occurred: {e}")
finally:
if conn:
conn.close()
print("Database connection closed.")