-
Notifications
You must be signed in to change notification settings - Fork 1
/
main2.py
71 lines (56 loc) · 2.43 KB
/
main2.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# using connection and db cursor as context managers
import psycopg2
from psycopg2 import OperationalError
from db_config import get_db_info
from fake_data import generate_fake_data
filename='db_info.ini'
section='postgres-sample-db'
db_info = get_db_info(filename,section)
# print(db_info)
connection = None
try:
with psycopg2.connect(**db_info) as db_connection:
print("Successfully connected to the database.")
# Create table
with db_connection.cursor() as db_cursor:
create_table = '''CREATE TABLE people(
id SERIAL PRIMARY KEY,
name varchar(50) NOT NULL,
city varchar(40),
profession varchar(60));'''
db_cursor.execute('DROP TABLE IF EXISTS people;')
db_cursor.execute(create_table)
# Insert one record
insert_record = 'INSERT INTO people (name,city,profession) VALUES (%s, %s, %s);'
insert_value = ('Jane Lee','RustMore','Rust programmer')
db_cursor.execute(insert_record, insert_value)
# Insert multiple records
records = tuple(generate_fake_data(100))
for record in records:
db_cursor.execute(insert_record,record)
# Retrieve data from the table
db_cursor.execute('SELECT * FROM people')
print(db_cursor.fetchone())
for record in db_cursor.fetchmany(10):
print(record)
for record in db_cursor.fetchall():
print(record)
get_count ='''SELECT city, COUNT(*)
FROM people
GROUP BY city HAVING COUNT(*)>1;'''
db_cursor.execute(get_count)
print(db_cursor.fetchall())
# Update records
update_query = 'UPDATE people SET city=%s WHERE city=%s;'
values = ('Mathville','Johnsonmouth')
db_cursor.execute(update_query,values)
# Delete records
delete_record = 'DELETE FROM people WHERE city=%s;'
record = ('Mathville',)
db_cursor.execute(delete_record,record)
except OperationalError:
print("Error connecting to the database :/")
finally:
if connection:
connection.close()
print("Closed connection.")