ajout méthode pour faire des requetes SQL

This commit is contained in:
Mylloon 2021-06-03 10:33:32 +02:00
parent 5ca726bf7c
commit fc805d9b8e

View file

@ -1,15 +1,23 @@
import sqlite3 import sqlite3
class Database: class Database:
def __init__(self): def __init__(self):
path = "src/db/bot.sqlite3" self.curseur = self.createConnection("src/db/bot.sqlite3").cursor()
if not self.isDatabaseExists(path):
self.createDB(path)
self.con = sqlite3.connect(path)
print("DB Open")
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: try:
open(path, "r") open(path, "r")
except FileNotFoundError: except FileNotFoundError:
@ -17,6 +25,9 @@ class Database:
else: else:
return True return True
def createDB(self, path): def requete(self, requete):
print("Creating DB...") """Reqête vers la base de données"""
open(path, "x") try:
self.curseur.execute(requete)
except sqlite3.Error as e:
print(e)