forked from SalN3t/NlpSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmySql_demon.py
More file actions
36 lines (31 loc) · 971 Bytes
/
mySql_demon.py
File metadata and controls
36 lines (31 loc) · 971 Bytes
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
import config
import MySQLdb
class DB():
def __init__(self):
self.db = MySQLdb.connect(**config.db_config)
def query(self, sql_query):
cur = self.db.cursor()
cur.execute(sql_query)
return cur
def query_pretty(self, sql_query):
cur = self.db.cursor()
try:
cur.execute(sql_query)
except Exception as e:
return str(e)
output = ''
numrows = cur.rowcount
# Get and display one row at a time
if numrows < 1:
return 'Sorry! No result founds in our database!'
for x in range(0, numrows):
row = cur.fetchone()
i = 0
for i in range(len(cur.description)):
output += '\n'
output += str(cur.description[i][0])+ " --> "+ str(row[i])
output += '\n'
output += '-'*25
return output
def close(self):
self.db.close()