-
Notifications
You must be signed in to change notification settings - Fork 4
/
postgres_service.py
32 lines (26 loc) · 1.08 KB
/
postgres_service.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
import psycopg2 as pg
class Postgres():
def __init__(self):
self.conn_string = "host='127.0.0.1' dbname='test' user='postgres' password=''"
self.conn = pg.connect(self.conn_string)
self.cursor = self.conn.cursor()
def populate(self):
self.cursor.execute("DROP TABLE IF EXISTS things;")
self.cursor.execute("CREATE TABLE things (name varchar(20));")
self.cursor.execute("INSERT INTO things(name) VALUES('Dre');")
self.cursor.execute("INSERT INTO things(name) VALUES('Smalls');")
self.cursor.execute("INSERT INTO things(name) VALUES('West');")
self.cursor.execute("INSERT INTO things(name) VALUES('Combs');")
self.cursor.execute("INSERT INTO things(name) VALUES('Flame');")
def read(self):
self.cursor.execute("SELECT * FROM things;")
count = self.cursor.fetchall()
return len(count)
def disconnect(self):
if self.conn:
self.conn.close()
if __name__ == '__main__':
postgres = Postgres()
postgres.populate()
postgres.read()
postgres.disconnect()