You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.3 KiB

3 years ago
#!/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
# * ----------------------------------------------------------------------------
# */
2 years ago
from configparser import ConfigParser
import os
3 years ago
import pymysql
import sys
3 years ago
3 years ago
# Read conf.ini
INI_CONF = "./config/mysql_conf.ini"
if os.path.exists(INI_CONF) is False:
print("MySQL configuration error, {}: file not found".format(INI_CONF))
sys.exit(1)
2 years ago
CONFIG = ConfigParser()
3 years ago
CONFIG.read(INI_CONF)
# DB parameters
SQL_HOST = CONFIG.get('mysql', 'host')
SQL_DB = CONFIG.get('mysql', 'database')
SQL_USER = CONFIG.get('mysql', 'user')
SQL_PWD = CONFIG.get('mysql', 'passwd')
3 years ago
3 years ago
class HandleSql:
"""Class to handle sql connection."""
3 years ago
3 years ago
@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()
return True
3 years ago
except pymysql.DatabaseError as error:
print(error)
3 years ago
3 years ago
@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
3 years ago
3 years ago
@staticmethod
def sql_delete(fsql_delete):
"""Sql delte request."""
# global cur
cur = conn.cursor()
cur.execute(fsql_delete)
conn.commit()
3 years ago
3 years ago
@staticmethod
def sql_insert(fsql_insert):
"""Sql insert request."""
# global cur
cur = conn.cursor()
cur.execute(fsql_insert)
conn.commit()
3 years ago
3 years ago
@staticmethod
def sql_unconn():
"""Deconnection sql."""
cur = conn.cursor()
cur.close()
conn.close()
3 years ago
if __name__ == "__main__":
3 years ago
print("Module sql to handle the connection")