This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
124 lines (112 loc) · 3.65 KB
/
database.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
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import json
import mysql.connector
from mysql.connector import errorcode
reload(sys)
sys.setdefaultencoding('utf8')
#General database methods
def connect(info):
"""Connect to the database, db is a dictionnary"""
try:
cnx = mysql.connector.connect(host=info["host"],
user=info["user"],
password=info["password"],
database=info["database"],
port=info["port"])
return cnx
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Invalid username or password")
return
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
return
else:
print(err)
return
def quit(cursor, cnx):
"""Close and quit the connection to the database"""
cursor.close()
cnx.close()
#AUTHORS TABLE
def getAuthorsCount(cnx):
"""Return the number in the AUTHORS tables"""
cursor = cnx.cursor()
query = "SELECT count(*) FROM AUTHORS;"
cursor.execute(query)
count = cursor.fetchone()
cursor.close()
return count[0]
def getAuthorInfosFromID(cnx,authID=0):
"""Return the author information from the AUTHORS table with its ID"""
author = dict()
cursor = cnx.cursor(dictionary=True)
query = "SELECT * FROM AUTHORS WHERE ID=%s;"
cursor.execute(query, (authID,))
for row in cursor:
author = row
cursor.close()
for key in author.keys():
if author[key] is None:
author[key] = ''
return author
def formatAuthor(author):
authStr = str()
"""Return the string format for an author struct"""
if author["SURNAME"] != '':
authStr = "{} ".format(author["SURNAME"])
authStr = authStr + "{}".format(author["NAME"])
if author["BIRTH"] != '':
authStr = authStr + " ( {} - {} )".format(author["BIRTH"], author["DEATH"])
return authStr
#QUOTES TABLE
def getQuotesCount(cnx):
"""Return the number in the QUOTES tables"""
cursor = cnx.cursor()
query = "SELECT count(*) FROM QUOTES;"
cursor.execute(query)
count = cursor.fetchone()
cursor.close()
return count[0]
def getQuoteInfosFromID(cnx,quoteID=0):
"""Return the quote information from the QUOTES tables with its ID"""
quote = dict()
cursor = cnx.cursor(dictionary=True)
query = "SELECT * FROM QUOTES WHERE ID=%s;"
cursor.execute(query, (quoteID,))
for row in cursor:
quote = row
cursor.close()
return quote
def getQuoteInfosFromAuthID(cnx,authID=0):
"""Return the quotes from the QUOTES table with an author ID"""
cursor = cnx.cursor(dictionary=True)
query = "SELECT * FROM QUOTES WHERE AUTH_ID=%s"
cursor.execute(query,(authID,))
quotes = list()
for quote in cursor:
quote["QUOTE"].decode('ascii','replace')
quotes.append(quote)
cursor.close()
return quotes
#MAIN
if __name__ == "__main__":
print("[Loading the database parameters] Connecting to the database...")
with open('connexionBDD.json') as db:
dbInfo = json.load(db)
connexion = connect(dbInfo)
print(dbInfo)
print("Connection established")
authCount = getAuthorsCount(connexion)
quotCount = getQuotesCount(connexion)
print("Authors count : {}".format(authCount))
print("Quotes count : {}".format(quotCount))
for i in range(1,authCount):
author = getAuthorInfosFromID(connexion,i)
quoteAuth = getQuoteInfosFromAuthID(connexion,i)
for j in range(len(quoteAuth)):
print(quoteAuth[j]["QUOTE"])
print("\n")
print("\n\n\n")