python-module-mysql/manage_sql/manage_sql.py

86 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Module sql connection."""
# /*
# * ----------------------------------------------------------------------------
# * "THE BEER-WARE LICENSE" (Revision 42):
# * <boris.tassou@securmail.fr> wrote this file. As long as you retain this notice you
# * can do whatever you want with this stuff. If we meet some day, and you think
# * this stuff is worth it, you can buy me a beer in return Boris Tassou
# * ----------------------------------------------------------------------------
# */
from configparser import SafeConfigParser
import pymysql
# Read conf.ini
INI_CONF = "./config/conf.ini"
CONFIG = SafeConfigParser()
CONFIG.read(INI_CONF)
# DB parameters
SQL_HOST = CONFIG.get('sql', 'host')
SQL_DB = CONFIG.get('sql', 'database')
SQL_USER = CONFIG.get('sql', 'user')
SQL_PWD = CONFIG.get('sql', 'passwd')
class HandleSql:
"""Class to handle sql connection."""
@staticmethod
def sql_checkcon():
"""Init sql connection."""
try:
global conn
conn = pymysql.connect(host=SQL_HOST, db=SQL_DB, user=SQL_USER, passwd=SQL_PWD)
cur = conn.cursor()
except pymysql.DatabaseError as error:
print(error)
@staticmethod
def sql_conn():
"""Init sql connection."""
try:
global conn
conn = pymysql.connect(host=SQL_HOST, db=SQL_DB, user=SQL_USER, passwd=SQL_PWD)
cur = conn.cursor()
except pymysql.DatabaseError as error:
print(error)
@staticmethod
def sql_select(fsql_select):
"""Sql select request."""
# global cur
cur = conn.cursor()
sql_return = cur.execute(fsql_select)
sql_return = cur.fetchall()
return sql_return
@staticmethod
def sql_delete(fsql_delete):
"""Sql delte request."""
# global cur
cur = conn.cursor()
cur.execute(fsql_delete)
conn.commit()
@staticmethod
def sql_insert(fsql_insert):
"""Sql insert request."""
# global cur
cur = conn.cursor()
cur.execute(fsql_insert)
conn.commit()
@staticmethod
def sql_unconn():
"""Deconnection sql."""
cur = conn.cursor()
cur.close()
conn.close()
if __name__ == "__main__":
print("Module sql to handle the connection")