MAJ module
This commit is contained in:
parent
44fd7d1a09
commit
9acf0f5a7e
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
conf.ini
|
15
README.md
Normal file
15
README.md
Normal 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
|
|
@ -19,7 +19,7 @@ __author__ = 'Boris Tassou <boris.tassou@securmail.fr>'
|
||||||
__license__ = 'Beerware'
|
__license__ = 'Beerware'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import MySQLdb
|
import pymysql
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print('MySQLdb library not found')
|
print('PyMSQL library not found')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
Binary file not shown.
Binary file not shown.
10
manage_sql/conf.ini
Executable file → Normal file
10
manage_sql/conf.ini
Executable file → Normal file
|
@ -1,5 +1,5 @@
|
||||||
[sql]
|
[sql]
|
||||||
host=localhost
|
host=localhost
|
||||||
database=postfix
|
database=adherents_services
|
||||||
user=postfix
|
user=root
|
||||||
passwd=blabli
|
passwd=Boris1989!
|
5
manage_sql/conf.ini.sample
Executable file
5
manage_sql/conf.ini.sample
Executable file
|
@ -0,0 +1,5 @@
|
||||||
|
[sql]
|
||||||
|
host=localhost
|
||||||
|
database=postfix
|
||||||
|
user=postfix
|
||||||
|
passwd=blabli
|
|
@ -12,59 +12,74 @@
|
||||||
# */
|
# */
|
||||||
|
|
||||||
from configparser import SafeConfigParser
|
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 HandleSql:
|
||||||
"""Class to handle sql connection."""
|
"""Class to handle sql connection."""
|
||||||
|
|
||||||
def sql_conn(self):
|
@staticmethod
|
||||||
"""Init sql connection."""
|
def sql_check():
|
||||||
# Read conf.ini
|
"""Init sql connection."""
|
||||||
ini_conf = "./config/conf.ini"
|
try:
|
||||||
config = SafeConfigParser()
|
global conn
|
||||||
config.read(ini_conf)
|
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
|
@staticmethod
|
||||||
sql_host = config.get('sql', 'host')
|
def sql_conn():
|
||||||
sql_db = config.get('sql', 'database')
|
"""Init sql connection."""
|
||||||
sql_user = config.get('sql', 'user')
|
try:
|
||||||
sql_pwd = config.get('sql', 'passwd')
|
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:
|
@staticmethod
|
||||||
global conn
|
def sql_select(fsql_select):
|
||||||
conn = MySQLdb.connect(host=sql_host, db=sql_db, user=sql_user, passwd=sql_pwd)
|
"""Sql select request."""
|
||||||
cur = conn.cursor()
|
# global cur
|
||||||
except (Exception, MySQLdb.DatabaseError) as error:
|
cur = conn.cursor()
|
||||||
print(error)
|
sql_return = cur.execute(fsql_select)
|
||||||
|
sql_return = cur.fetchall()
|
||||||
|
return sql_return
|
||||||
|
|
||||||
def sql_select(self, sql_select):
|
@staticmethod
|
||||||
"""Sql select request."""
|
def sql_delete(fsql_delete):
|
||||||
# global cur
|
"""Sql delte request."""
|
||||||
cur = conn.cursor()
|
# global cur
|
||||||
sql_return = cur.execute(sql_select)
|
cur = conn.cursor()
|
||||||
sql_return = cur.fetchall()
|
cur.execute(fsql_delete)
|
||||||
return sql_return
|
conn.commit()
|
||||||
|
|
||||||
def sql_delete(self, sql_delete):
|
@staticmethod
|
||||||
"""Sql delte request."""
|
def sql_insert(fsql_insert):
|
||||||
# global cur
|
"""Sql insert request."""
|
||||||
cur = conn.cursor()
|
# global cur
|
||||||
cur.execute(sql_delete)
|
cur = conn.cursor()
|
||||||
conn.commit()
|
cur.execute(fsql_insert)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
def sql_insert(self, sql_insert):
|
@staticmethod
|
||||||
"""Sql insert request."""
|
def sql_unconn():
|
||||||
# global cur
|
"""Deconnection sql."""
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
cur.execute(sql_insert)
|
cur.close()
|
||||||
conn.commit()
|
conn.close()
|
||||||
|
|
||||||
def sql_unconn(self):
|
|
||||||
"""Deconnection sql."""
|
|
||||||
cur = conn.cursor()
|
|
||||||
cur.close()
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("Module sql to handle the connection")
|
print("Module sql to handle the connection")
|
||||||
|
|
|
@ -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
9
test.py
Normal 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
9
test_conf.py
Normal 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)
|
Loading…
Reference in a new issue