forked from grifon/python-module-mysql
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'
|
||||
|
||||
try:
|
||||
import MySQLdb
|
||||
import pymysql
|
||||
except ImportError:
|
||||
print('MySQLdb library not found')
|
||||
print('PyMSQL library not found')
|
||||
sys.exit(1)
|
||||
|
|
Binary file not shown.
Binary file not shown.
6
manage_sql/conf.ini
Executable file → Normal file
6
manage_sql/conf.ini
Executable file → Normal file
|
@ -1,5 +1,5 @@
|
|||
[sql]
|
||||
host=localhost
|
||||
database=postfix
|
||||
user=postfix
|
||||
passwd=blabli
|
||||
database=adherents_services
|
||||
user=root
|
||||
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,55 +12,70 @@
|
|||
# */
|
||||
|
||||
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):
|
||||
@staticmethod
|
||||
def sql_check():
|
||||
"""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)
|
||||
conn = pymysql.connect(host=SQL_HOST, db=SQL_DB, user=SQL_USER, passwd=SQL_PWD)
|
||||
cur = conn.cursor()
|
||||
except (Exception, MySQLdb.DatabaseError) as error:
|
||||
except pymysql.DatabaseError as error:
|
||||
print(error)
|
||||
|
||||
def sql_select(self, sql_select):
|
||||
@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)
|
||||
|
||||
@staticmethod
|
||||
def sql_select(fsql_select):
|
||||
"""Sql select request."""
|
||||
# global cur
|
||||
cur = conn.cursor()
|
||||
sql_return = cur.execute(sql_select)
|
||||
sql_return = cur.execute(fsql_select)
|
||||
sql_return = cur.fetchall()
|
||||
return sql_return
|
||||
|
||||
def sql_delete(self, sql_delete):
|
||||
@staticmethod
|
||||
def sql_delete(fsql_delete):
|
||||
"""Sql delte request."""
|
||||
# global cur
|
||||
cur = conn.cursor()
|
||||
cur.execute(sql_delete)
|
||||
cur.execute(fsql_delete)
|
||||
conn.commit()
|
||||
|
||||
def sql_insert(self, sql_insert):
|
||||
@staticmethod
|
||||
def sql_insert(fsql_insert):
|
||||
"""Sql insert request."""
|
||||
# global cur
|
||||
cur = conn.cursor()
|
||||
cur.execute(sql_insert)
|
||||
cur.execute(fsql_insert)
|
||||
conn.commit()
|
||||
|
||||
def sql_unconn(self):
|
||||
@staticmethod
|
||||
def sql_unconn():
|
||||
"""Deconnection sql."""
|
||||
cur = conn.cursor()
|
||||
cur.close()
|
||||
|
|
|
@ -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