common -> app.common.

This commit is contained in:
hsafaei
2025-09-18 14:32:19 +03:30
parent a507fff651
commit b4d5671809
83 changed files with 192 additions and 163 deletions
+46
View File
@@ -0,0 +1,46 @@
import mariadb
from dotenv import load_dotenv
import os
class sql_helper:
def __init__(self):
config_env = load_dotenv(dotenv_path="./config.env")
MYSQL_username = os.getenv("MYSQL_username")
MYSQL_root_password = os.getenv("MYSQL_root_password")
MYSQL_port = os.getenv("MYSQL_port")
MYSQL_host = os.getenv("MYSQL_import_host")
self.cnx = mariadb.connect(user="root", password=MYSQL_root_password, host=MYSQL_host)
self.cursor = self.cnx.cursor()
def create_users(self):
password = os.getenv("MYSQL_password")
users = os.getenv("MYSQL_username")
try:
self.cursor.execute(f"CREATE USER '{users}'@'%' IDENTIFIED BY '{password}'")
self.cursor.execute(f" GRANT ALL PRIVILEGES ON *.* to '{users}'@'%'")
print("create user")
except:
print("not create user")
def read_file_import(self,filename,name_database):
self.cursor.execute(f"use {name_database}")
with open(filename, 'r') as file:
sqlFile = file.read()
file.close()
sqlCommands = sqlFile.split(';\n')
for command in sqlCommands:
try:
if command.rstrip() != '':
self.cursor.execute(command)
self.cnx.commit()
except ValueError as msg:
print ("Command skipped: ", msg)
def create_database(self,name_database):
try:
self.cursor.execute(f"CREATE DATABASE {name_database};")
self.cnx.commit()
print(f"create database {name_database}")
except:
print(f"not create database {name_database}")