-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
52 lines (47 loc) · 1.76 KB
/
db.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
import sqlite3
import datetime
import logging
__author__ = 'HK Dambanemuya'
__version__ = 'Python2'
'''
Database class for reading and writing queries and responses
'''
class Database():
def __init__(self, file_path):
self.file_path = file_path
# Open database connection
connection = sqlite3.connect(file_path)
# Read SQL query to create table
with open('create_tables.sql', 'r') as fd:
sqlFile = fd.read()
# Execute SQL query
c = connection.cursor()
c.execute(sqlFile)
# Close database connection
connection.close()
def write_response(self, source, message, response):
# Read SQL query to write response
with open('write_response.sql', 'r') as fd:
sqlFile = fd.read()
# Write transcribed smart speaker response to database
try:
# Open database connection
connection = sqlite3.connect(self.file_path)
c = connection.cursor()
# Format query tuple
query_tuple = (source, message, response)
# Execute SQL query
c.execute(sqlFile, query_tuple)
# Commit changes to database
connection.commit()
# Handle and save exceptions in error log
except Exception as e:
logging.info(e)
with open('error_' + datetime.datetime.now().strftime('%Y%m%d%H%M%S'),'w') as file:
file.write(logging.info(e))
file.write(source)
file.write(message)
file.write(response)
logging.basicConfig(level=logging.INFO,
format='%(asctime)s.%(msecs)03d %(levelname)s {%(module)s} [%(funcName)s] %(message)s',
datefmt='%Y-%m-%d,%H:%M:%S')