-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabaseConnection.py
48 lines (39 loc) · 1.41 KB
/
databaseConnection.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
import pymysql.cursors
class DBConnection:
def __init__(self):
self.connected = False
self.connection = pymysql.connections
def database_connection(self):
try:
conn = pymysql.connect(host='localhost',
user='root',
password='vuota',
db='census',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
self.connection = conn
self.connected = True
print('\n Database connected! \n')
except ValueError as ve:
print(ve)
print("Connection problem!")
def query(self, query):
if self.connected:
try:
with self.connection.cursor() as cursor:
sql = query
''' SELECT * FROM censusdata WHERE id=1 '''
cursor.execute(sql)
result = cursor.fetchall()
return result
except ValueError as ve:
print(ve)
else:
print("Database is not connected")
def return_connection(self):
if self.connected:
return self.connection
def close_connection(self):
if self.connected:
self.connected = False
self.connection.close()