Browse Source

MAJ module

pull/1/head
Boris Tassou 3 years ago
parent
commit
9acf0f5a7e
  1. 1
      .gitignore
  2. 15
      README.md
  3. 4
      manage_sql/__init__.py
  4. BIN
      manage_sql/__pycache__/__init__.cpython-36.pyc
  5. BIN
      manage_sql/__pycache__/manage_sql.cpython-36.pyc
  6. 10
      manage_sql/conf.ini
  7. 5
      manage_sql/conf.ini.sample
  8. 105
      manage_sql/manage_sql.py
  9. 21
      manage_sql/test.py
  10. 9
      test.py
  11. 9
      test_conf.py

1
.gitignore vendored

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

15
README.md

@ -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

4
manage_sql/__init__.py

@ -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)

BIN
manage_sql/__pycache__/__init__.cpython-36.pyc

Binary file not shown.

BIN
manage_sql/__pycache__/manage_sql.cpython-36.pyc

Binary file not shown.

10
manage_sql/conf.ini

@ -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

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

105
manage_sql/manage_sql.py

@ -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."""
def sql_conn(self):
"""Init sql connection."""
# Read conf.ini
ini_conf = "./config/conf.ini"
config = SafeConfigParser()
config.read(ini_conf)
class HandleSql:
"""Class to handle sql connection."""
# 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_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)
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_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)
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_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_delete(self, sql_delete):
"""Sql delte request."""
# global cur
cur = conn.cursor()
cur.execute(sql_delete)
conn.commit()
@staticmethod
def sql_delete(fsql_delete):
"""Sql delte request."""
# global cur
cur = conn.cursor()
cur.execute(fsql_delete)
conn.commit()
def sql_insert(self, sql_insert):
"""Sql insert request."""
# global cur
cur = conn.cursor()
cur.execute(sql_insert)
conn.commit()
@staticmethod
def sql_insert(fsql_insert):
"""Sql insert request."""
# global cur
cur = conn.cursor()
cur.execute(fsql_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")

21
manage_sql/test.py

@ -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

@ -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

@ -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…
Cancel
Save