-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_connection.py
68 lines (58 loc) · 1.8 KB
/
db_connection.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
import psycopg2
from prettytable import from_db_cursor
from config import config
class Database:
def __init__(self):
#initiate connection to postgres db
self.params = config()
print("Connecting to postgresql database...")
self.connection = psycopg2.connect(**self.params)
#create cursor
self.cursor = self.connection.cursor()
# check if table exists already
self.cursor.execute(
'''
SELECT EXISTS (
SELECT FROM
pg_tables
WHERE
schemaname = 'public' AND
tablename = 'table1'
);
'''
)
self.table_exists = self.cursor.fetchone()
if self.table_exists[0] == False:
self.cursor.execute(
'''
create table table1(
id int unique not null primary key
, name text
, age int
);
'''
)
self.submit()
#print database version
self.cursor.execute('Select version()')
self.db_version = self.cursor.fetchone()
print(self.db_version)
def return_data(self):
self.cursor.execute("select * from table1 order by id;")
return(self.cursor.fetchall())
def primary_key(self):
#fetch last id
self.cursor.execute("select id from table1 where id = (select max(id) from table1)")
self.last_id = self.cursor.fetchone()
if self.last_id == None:
self.next_id = 1
else:
self.next_id = self.last_id[0] + 1
return self.next_id
def submit(self):
self.connection.commit()
print("Update commited to db")
def terminate(self):
self.cursor.close()
self.connection.close()
print("Connection to database terminated.")