ajout méthode pour faire des requetes SQL
This commit is contained in:
parent
5ca726bf7c
commit
fc805d9b8e
1 changed files with 21 additions and 10 deletions
|
@ -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)
|
||||||
|
|
Reference in a new issue