-
Notifications
You must be signed in to change notification settings - Fork 5
/
intro_to_psycopg2.py
93 lines (79 loc) · 2.42 KB
/
intro_to_psycopg2.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import psycopg2
# Create a cursor.
pg_conn_string = os.environ["PG_CONN_STRING"]
connection = psycopg2.connect(pg_conn_string)
cursor = connection.cursor()
# Create a table
def create_tables():
cursor.execute("CREATE TABLE courses ( \
id UUID NOT NULL DEFAULT gen_random_uuid(),\
name STRING NOT NULL,\
program STRING NOT NULL, \
code INT NOT NULL, \
credits DECIMAL NULL \
)")
connection.commit()
# Insert data into a table.
def insert_data():
cursor.execute(
"INSERT INTO courses VALUES (default, 'Distributed Systems', 'ece', 454, 1.5)"
)
cursor.execute(
"INSERT INTO courses (name, program, code) VALUES ('Databases', 'cs', 348)"
)
cursor.execute(
"INSERT INTO courses (name, program, code, credits) VALUES ('Programming for Performance', 'ece', 459, 1)"
)
connection.commit()
# Update data in a table.
def update_rows():
cursor.execute("UPDATE courses SET program = 'ECE' where program = 'ece'")
cursor.execute("UPDATE courses SET program = 'CS' where program = 'cs'")
connection.commit()
# Delete rows.
def delete_rows():
cursor.execute("DELETE FROM courses WHERE code = 459")
connection.commit()
def alter_table():
cursor.execute("ALTER TABLE courses DROP COLUMN credits")
connection.commit()
cursor.execute("ALTER TABLE courses ADD COLUMN credits INT DEFAULT 1")
connection.commit()
# Query a table.
def select_all():
cursor.execute("SELECT * FROM courses")
results = cursor.fetchall()
connection.commit()
print(results)
print('\n')
def select_some_with_params():
cursor.execute("SELECT * FROM courses WHERE program = %s", ('ECE',))
results = cursor.fetchall()
connection.commit()
print(results)
print('\n')
# Drop table.
def drop_tables():
cursor.execute("DROP TABLE courses")
connection.commit()
def add_course_with_params():
cursor.execute("INSERT INTO courses VALUES (default, %s, %s, %s, %s)",
("Algorithms", "CS", "341", 1))
connection.commit()
def add_course_with_named_params():
data = {
'name': 'Programming for Performance',
'code': '459',
'program': 'ECE',
}
cursor.execute("INSERT INTO courses VALUES (default, %(name)s, %(program)s, %(code)s)", data)
create_tables()
# insert_data()
# update_rows()
# delete_rows()
# alter_table()
# add_course_with_params()
# select_all()
# select_some_with_params()
# drop_tables()