-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_database.py
More file actions
38 lines (24 loc) · 1.07 KB
/
Copy pathtest_database.py
File metadata and controls
38 lines (24 loc) · 1.07 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
import psycopg2
#connect to database
conn = psycopg2.connect("dbname=hongkongmei user=postgres password=[insert postgres password here]")
#Create a cursor to perform db actions
cur = conn.cursor()
##########################################
# Resets database to rebuild
###########################################
cur.execute("DROP SCHEMA public CASCADE;")
cur.execute("CREATE SCHEMA public;")
cur.execute("GRANT ALL ON SCHEMA public TO postgres;")
cur.execute("GRANT ALL ON SCHEMA public TO public;")
###########################################
#example commands
#create a table with 3 columns, id which increments with each entr, num which takes an integer and data which takes a string
cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
#Insert a row into the table. %s denotes an entry to be added. This santizes the inputs to prevent sql injections
cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100, "abc'def"))
#make changes persistent
conn.commit()
#close communication with the database
cur.close()
conn.close()
print("hello")