-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
133 lines (116 loc) · 4.59 KB
/
database.py
File metadata and controls
133 lines (116 loc) · 4.59 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""DataBase.
This module handles all the databases related activities.
"""
import logging
from typing import Union
import mysql.connector as connection
from logger import AppLogger
class DataBase:
"""Handle database activities."""
def __init__(self) -> None:
"""Intialize required variables."""
self.__is_client_connected = False
self.__db = None
self.__logger_class = AppLogger()
self.__logger = self.__logger_class.get_logger(name="database")
self.__tablename = "reviews"
def db_connect(self, host="127.0.0.1", user="root", passwd="root") -> bool:
"""Connect to database.
Args:
host (str, optional): ip address. Defaults to "127.0.0.1".
user (str, optional): username. Defaults to "root".
passwd (str, optional): password. Defaults to "root".
tablename (str, optional):tablename. Defaults to reviews
Returns:
bool: True on success, False on failure
"""
try:
self.__db = connection.connect(
host=host, user=user, passwd=passwd, use_pure=True
)
if self.__db.is_connected() is False:
self.__db = None
else:
self.__is_client_connected = True
self.__logger.info("Connected to DB successfully")
query = "CREATE DATABASE IF NOT EXISTS Flipkart"
cur = self.__db.cursor()
cur.execute(query)
self.__db.commit()
except Exception as db_exception:
self.__db = None
self.__logger.exception(str(db_exception))
return self.__is_client_connected
def create_table(self, tablename):
"""Create a table if not exists.
Args:
tablename (str): tablename
"""
if self.__is_client_connected is True:
self.__tablename = tablename
try:
cur = self.__db.cursor()
query = f"CREATE TABLE IF NOT EXISTS Flipkart.{self.__tablename}(Name VARCHAR(50),Rating VARCHAR(5), CommentHead VARCHAR(100), Comment VARCHAR(1000))"
cur.execute(query)
self.__db.commit()
except Exception as db_exception:
self.__logger.exception(str(db_exception))
else:
self.__logger.warning("Database is not connected")
def add_to_db(self, data) -> None:
"""Add given data to db.
Args:
data (list): data
"""
if self.__is_client_connected is True:
try:
cur = self.__db.cursor()
query = f'INSERT INTO Flipkart.{self.__tablename} VALUES("{data[0]}","{data[1]}","{data[2]}","{data[3]}")'
cur.execute(query)
self.__db.commit()
except Exception as db_add_exception:
self.__logger.exception(str(db_add_exception))
else:
self.__logger.info("Database is not connected")
def check_if_table_exists(self, tablename) -> bool:
"""Check if a table already exists.
Args:
tablename (str): product name
Returns:
bool: True if exists else False
"""
if self.__is_client_connected is True:
cur = self.__db.cursor()
query = f"SELECT COUNT(*) FROM information_schema.tables where table_name='{tablename}'"
cur.execute(query)
if cur.fetchone()[0] == 1:
return True
else:
return False
else:
return True
def get_data(self, tablename) -> Union[list, None]:
"""Get data if it exists.
Args:
tablename (str): tablename
Returns:
list | None: list if success else None
"""
if self.__is_client_connected is True:
try:
cur = self.__db.cursor()
query = f"SELECT * FROM Flipkart.{tablename}"
cur.execute(query)
data = list(cur.fetchall())
self.__logger.info("Reusing the data from db")
return data
except Exception as db_exception:
self.__logger.exception(db_exception)
return None
else:
return None
def close_db(self) -> None:
"""Close database."""
if self.__is_client_connected is True:
self.__db.close()
self.__logger.info("Closed DB successfully")