summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--DBcm.py17
-rw-r--r--vsearch4web.py13
2 files changed, 24 insertions, 6 deletions
diff --git a/DBcm.py b/DBcm.py
new file mode 100644
index 0000000..afe2391
--- /dev/null
+++ b/DBcm.py
@@ -0,0 +1,17 @@
+import mysql.connector
+
+
+class UseDatabase:
+
+ def __init__(self, config: dict) -> None:
+ self.configuration = config
+
+ def __enter__(self) -> 'cursor':
+ self.conn = mysql.connector.connect(**self.configuration)
+ self.cursor = self.conn.cursor()
+ return self.cursor
+
+ def __exit__(self, exc_type, exc_value, exc_trace) -> None:
+ self.conn.commit()
+ self.cursor.close()
+ self.conn.close()
diff --git a/vsearch4web.py b/vsearch4web.py
index db44936..3eed663 100644
--- a/vsearch4web.py
+++ b/vsearch4web.py
@@ -1,19 +1,20 @@
from flask import Flask, render_template, request, escape
from vsearch import search4letters
-import mysql.connector
+
+from DBcm import UseDatabase
app = Flask(__name__)
-dbconfig = {'host': '127.0.0.1',
- 'user': 'vsearch',
- 'password': 'vsearchpasswd',
- 'database': 'vsearchlogDB', }
+app.config['dbconfig'] = {'host': '127.0.0.1',
+ 'user': 'vsearch',
+ 'password': 'vsearchpasswd',
+ 'database': 'vsearchlogDB', }
def log_request(req: 'flask_request', res: str) -> None:
"""Log details of the web request and the results."""
- with UseDatabase(dbconfig) as cursor:
+ with UseDatabase(app.config['dbconfig']) as cursor:
_SQL = """insert into log
(phrase, letters, ip, browser_string, results)
values