This repository has been archived on 2022-06-13. You can view files and clone it, but cannot push or open issues or pull requests.
KassouBot/src/utils/db.py

39 lines
1.1 KiB
Python
Raw Normal View History

2021-06-03 08:07:53 +02:00
import sqlite3
2021-06-03 08:40:40 +02:00
class Database:
2021-06-03 08:30:17 +02:00
def __init__(self):
2021-06-03 10:54:03 +02:00
self.connexion = self.createConnection(r"src/db/bot.sqlite3")
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)
except sqlite3.Error as e:
print(e)
return connnexion
2021-06-03 08:30:17 +02:00
def isFileExists(self, path):
"""Vérifie qu'un fichier existe"""
2021-06-03 08:30:17 +02:00
try:
2021-06-03 09:03:01 +02:00
open(path, "r")
2021-06-03 08:30:17 +02:00
except FileNotFoundError:
return False
else:
return True
2021-06-03 10:54:03 +02:00
def requete(self, requete, valeurs = None):
"""Envois une requête vers la base de données"""
try:
2021-06-03 11:41:30 +02:00
curseur = self.connexion.cursor()
2021-06-03 10:54:03 +02:00
if valeurs:
2021-06-03 11:41:30 +02:00
curseur.execute(requete, valeurs)
2021-06-03 10:54:03 +02:00
else:
2021-06-03 11:41:30 +02:00
curseur.execute(requete)
2021-06-03 10:54:03 +02:00
self.connexion.commit()
2021-06-03 11:41:30 +02:00
curseur.close()
except sqlite3.Error as e:
print(e)