71 lines
1.9 KiB
Python
Executable File
71 lines
1.9 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 MySQLdb
|
|
|
|
|
|
class HandleSql:
|
|
"""Class to handle sql connection."""
|
|
|
|
def sql_conn(self):
|
|
"""Init sql connection."""
|
|
# 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')
|
|
|
|
try:
|
|
global conn
|
|
conn = MySQLdb.connect(host=sql_host, db=sql_db, user=sql_user, passwd=sql_pwd)
|
|
cur = conn.cursor()
|
|
except (Exception, MySQLdb.DatabaseError) as error:
|
|
print(error)
|
|
|
|
def sql_select(self, sql_select):
|
|
"""Sql select request."""
|
|
# global cur
|
|
cur = conn.cursor()
|
|
sql_return = cur.execute(sql_select)
|
|
sql_return = cur.fetchall()
|
|
return sql_return
|
|
|
|
def sql_delete(self, sql_delete):
|
|
"""Sql delte request."""
|
|
# global cur
|
|
cur = conn.cursor()
|
|
cur.execute(sql_delete)
|
|
conn.commit()
|
|
|
|
def sql_insert(self, sql_insert):
|
|
"""Sql insert request."""
|
|
# global cur
|
|
cur = conn.cursor()
|
|
cur.execute(sql_insert)
|
|
conn.commit()
|
|
|
|
def sql_unconn(self):
|
|
"""Deconnection sql."""
|
|
cur = conn.cursor()
|
|
cur.close()
|
|
conn.close()
|
|
|
|
if __name__ == "__main__":
|
|
print("Module sql to handle the connection")
|