This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBsearch.py
48 lines (38 loc) · 1.46 KB
/
DBsearch.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
from flask import Flask, jsonify , request , render_template
from flask_sqlalchemy import SQLAlchemy
"""
DBcm stand for data base context manegar
ch 9 head first python
with UseDataBase(app.config['dbconfig']) as cursor:
"""
The use db class except to recive a dic of database conection.
Log details of the web request and the results.
"""
_SQL = """insert into log
(phrase, letters, ip, browser_string, results)
values
(%s, %s, %s, %s, %s)"""
cursor.execute(_SQL, (req.form['phrase'],
req.form['letters'],
req.remote_addr,
req.user_agent.browser,
res, ))
"""
import mysql.connector
class UseDataBase:
"""
We Know that, in order for our class to create a context manger it has to:
1- provide a __init__ `init dender` to provide and perform initialization
2- provide __enter__ `enter dender` to include any setup code
3- privide __exit__ `exit dender` to include any tear/shutdown code
"""
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()