diff options
| author | Willem Renes <wrenes@gmail.com> | 2021-05-12 12:07:27 +0200 |
|---|---|---|
| committer | Willem Renes <wrenes@gmail.com> | 2021-05-12 12:07:27 +0200 |
| commit | 6dabf79eba1d9a2bcee77687785280cb9bdd38ac (patch) | |
| tree | 16d3113e79801b55ceae5d944986d85c5df2bc54 | |
| parent | 20abdbd4c66ea086d809aea9be8a8aa8e3eba208 (diff) | |
| download | vsearchweb-master.tar.gz vsearchweb-master.zip | |
| -rw-r--r-- | DBcm.py | 17 | ||||
| -rw-r--r-- | vsearch4web.py | 13 |
2 files changed, 24 insertions, 6 deletions
@@ -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 |
