#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Module sql connection.""" # /* # * ---------------------------------------------------------------------------- # * "THE BEER-WARE LICENSE" (Revision 42): # * 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 ConfigParser import os import pymysql import sys # 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) CONFIG = ConfigParser() 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') class HandleSql: """Class to handle sql connection.""" @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 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")