MAJ module

This commit is contained in:
Boris Tassou 2019-12-03 12:12:41 +01:00
parent 44fd7d1a09
commit 9acf0f5a7e
11 changed files with 106 additions and 73 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
conf.ini

15
README.md Normal file
View File

@ -0,0 +1,15 @@
## Manage MySQL
Module for Python3 only.
On Ubuntu :
apt install libmysqlclient-dev
Install :
pip3 install mysqlclient
### Import
To import this module :
Copy the module folder in your working folder
In your python script : from manage_sql.manage_sql import HandleSql

View File

@ -19,7 +19,7 @@ __author__ = 'Boris Tassou <boris.tassou@securmail.fr>'
__license__ = 'Beerware'
try:
import MySQLdb
import pymysql
except ImportError:
print('MySQLdb library not found')
print('PyMSQL library not found')
sys.exit(1)

10
manage_sql/conf.ini Executable file → Normal file
View File

@ -1,5 +1,5 @@
[sql]
host=localhost
database=postfix
user=postfix
passwd=blabli
[sql]
host=localhost
database=adherents_services
user=root
passwd=Boris1989!

5
manage_sql/conf.ini.sample Executable file
View File

@ -0,0 +1,5 @@
[sql]
host=localhost
database=postfix
user=postfix
passwd=blabli

View File

@ -12,59 +12,74 @@
# */
from configparser import SafeConfigParser
import MySQLdb
import pymysql
# Read conf.ini
INI_CONF = "./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."""
"""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)
@staticmethod
def sql_check():
"""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)
# 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')
@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)
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)
@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
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
@staticmethod
def sql_delete(fsql_delete):
"""Sql delte request."""
# global cur
cur = conn.cursor()
cur.execute(fsql_delete)
conn.commit()
def sql_delete(self, sql_delete):
"""Sql delte request."""
# global cur
cur = conn.cursor()
cur.execute(sql_delete)
conn.commit()
@staticmethod
def sql_insert(fsql_insert):
"""Sql insert request."""
# global cur
cur = conn.cursor()
cur.execute(fsql_insert)
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()
@staticmethod
def sql_unconn():
"""Deconnection sql."""
cur = conn.cursor()
cur.close()
conn.close()
if __name__ == "__main__":
print("Module sql to handle the connection")
print("Module sql to handle the connection")

View File

@ -1,21 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from configparser import SafeConfigParser
import psycopg2
# Read conf.ini
ini_conf = "./conf.ini"
config = SafeConfigParser()
config.read(ini_conf)
# DB parameters
psql_host = config.get('psql', 'host')
psql_db = config.get('psql', 'database')
psql_user = config.get('psql', 'user')
psql_pwd = config.get('psql', 'passwd')
print(psql_host)
print(psql_db)
print(psql_user)
print(psql_pwd)

9
test.py Normal file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from manage_sql.manage_sql import HandleSql
MY_PSQL_CONN = HandleSql()
check_return_bdd = MY_PSQL_CONN.psql_checkcon()
print(check_return_bdd)

9
test_conf.py Normal file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env python3
import configparser as cp
config = cp.ConfigParser()
config.read('manage_sql/conf.ini')
valeur = config.get('sql', 'host')
print(valeur)