From fc805d9b8ef2febffe64da79e3e49169fa9c880b Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 3 Jun 2021 10:33:32 +0200 Subject: [PATCH] =?UTF-8?q?ajout=20m=C3=A9thode=20pour=20faire=20des=20req?= =?UTF-8?q?uetes=20SQL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/db.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/utils/db.py b/src/utils/db.py index 6a7f21e..dd45314 100644 --- a/src/utils/db.py +++ b/src/utils/db.py @@ -1,15 +1,23 @@ import sqlite3 class Database: - def __init__(self): - path = "src/db/bot.sqlite3" - if not self.isDatabaseExists(path): - self.createDB(path) - self.con = sqlite3.connect(path) - print("DB Open") + self.curseur = self.createConnection("src/db/bot.sqlite3").cursor() - def isDatabaseExists(self, path): + def createConnection(self, path): + """Connexion à une base de donnée SQLite""" + if not self.isFileExists(path): + open(path, "x") + connnexion = None + try: + connnexion = sqlite3.connect(path) + print(f"Database connected with SQLite v{sqlite3.version}") + except sqlite3.Error as e: + print(e) + return connnexion + + def isFileExists(self, path): + """Vérifie qu'un fichier existe""" try: open(path, "r") except FileNotFoundError: @@ -17,6 +25,9 @@ class Database: else: return True - def createDB(self, path): - print("Creating DB...") - open(path, "x") + def requete(self, requete): + """Reqête vers la base de données""" + try: + self.curseur.execute(requete) + except sqlite3.Error as e: + print(e)