From 9acf0f5a7e7f1aa007e06169332621eccf91d7be Mon Sep 17 00:00:00 2001 From: Boris Tassou Date: Tue, 3 Dec 2019 12:12:41 +0100 Subject: [PATCH] MAJ module --- .gitignore | 1 + README.md | 15 +++ manage_sql/__init__.py | 4 +- .../__pycache__/__init__.cpython-36.pyc | Bin 442 -> 448 bytes .../__pycache__/manage_sql.cpython-36.pyc | Bin 1859 -> 2198 bytes manage_sql/conf.ini | 10 +- manage_sql/conf.ini.sample | 5 + manage_sql/manage_sql.py | 105 ++++++++++-------- manage_sql/test.py | 21 ---- test.py | 9 ++ test_conf.py | 9 ++ 11 files changed, 106 insertions(+), 73 deletions(-) create mode 100644 .gitignore create mode 100644 README.md mode change 100755 => 100644 manage_sql/conf.ini create mode 100755 manage_sql/conf.ini.sample delete mode 100644 manage_sql/test.py create mode 100644 test.py create mode 100644 test_conf.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..93ec730 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +conf.ini \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d8996a7 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/manage_sql/__init__.py b/manage_sql/__init__.py index 8d9a827..629eb80 100755 --- a/manage_sql/__init__.py +++ b/manage_sql/__init__.py @@ -19,7 +19,7 @@ __author__ = 'Boris Tassou ' __license__ = 'Beerware' try: - import MySQLdb + import pymysql except ImportError: - print('MySQLdb library not found') + print('PyMSQL library not found') sys.exit(1) diff --git a/manage_sql/__pycache__/__init__.cpython-36.pyc b/manage_sql/__pycache__/__init__.cpython-36.pyc index 38d6fb53fc64007a3aff8c92ec62fe4a8b45f1da..21d5920f6b6af40729c029cb44ae595fb328c2bf 100644 GIT binary patch delta 93 zcmdnRe1MtLn3tC;$njaM;6%>j>=FT$zQKV$6L0vl7gXj}78mAB)?-vRaMRDo&rQ`& t&#cPL*LTTJF3nBND=F3wNG&SP*Dt6n$;i*s&CO3K%}LdTXsTnh0RY3sAqD^d delta 87 zcmX@Wyo;IBn3tEUwO=4gXd>rv4oTn2;6R_0q>0!4*`dtIx{T_Y_WGs8Mfw@}xvBc; lnN_*@`oXEmrA4`knK`nZ+eVi6xo&d6QchZ2-x;9m)Uz diff --git a/manage_sql/__pycache__/manage_sql.cpython-36.pyc b/manage_sql/__pycache__/manage_sql.cpython-36.pyc index 43efb6f8ecfd1860a303da4b9aef14a57a5b8515..9eebd3191928faafd2ee10bcce80acf883afb79e 100644 GIT binary patch literal 2198 zcmd5--EQ1O6rLG-ZLin6Y12@CMB4?#g%RDo0Ertws7Yvo=r(0R36_xMdL~&X{@ILY z1+kLeq_;c(kH9OMbLPw^-LAX&;Lo3aYZLMZ zS-K3sU%_2I1H(x`IOQy*84DOiY@|kDa5FFkyCgwNw2m!dUQ$W`*}TQAOBOhDYy)o3 zu>;th<2K;VL&9C&c}{o-o;=az-U*YH=c?@AxzG=@tj;zP;GLPw`vUPxy9`o+khS3uCW8S)(8G= zz#i|`<1X)kvNhe_iE^Hb@o8G!+f0E+UlsjHJ+`WL!H)r@8*p#JUG9N7yQ|1CRZJPm zR4q<_H6u$|$Mz+;Ap0c$*d#FToVfBEN-iL(RZq3gDaLo7{766#yP(hL0m<0`JOMAG z+Stt#waUOZr3s(Yj6anY0BM66`L4F7XW1DB*q&!cJL4zchC7eP-)jre)`Mo`hw=7z z+QyN+{Vi>xx3=P$EDNc-Tl2JRO9_vysZ4U^Gi|`UZV4Rxoygnp$-j4olOhwtqom4; z;Z_mPGLfrtxF=wt;q**Riu`_7m(2Z|*D#Cn=tzV>aS0BlXSxkTVLTD>Ngoz2T`^omRIN*%>VS{x@lmuxH5&d5}n7cF=fex@dZ6*3e+h$eU=^(cA*#Gr58I4Kx_q z#=HqgJGErU&`0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I zKHd1ZTsCxjqBlM@y~un5tGEMlXoYk4m2u6?P=|Le4c$=l7Kv-{)bgu_-q#eqY(w~4 L9WK2a_ay%ZE_}Rj diff --git a/manage_sql/conf.ini b/manage_sql/conf.ini old mode 100755 new mode 100644 index 7554ff5..4651b6d --- a/manage_sql/conf.ini +++ b/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! \ No newline at end of file diff --git a/manage_sql/conf.ini.sample b/manage_sql/conf.ini.sample new file mode 100755 index 0000000..7554ff5 --- /dev/null +++ b/manage_sql/conf.ini.sample @@ -0,0 +1,5 @@ +[sql] +host=localhost +database=postfix +user=postfix +passwd=blabli diff --git a/manage_sql/manage_sql.py b/manage_sql/manage_sql.py index 815510b..5b3fe3e 100755 --- a/manage_sql/manage_sql.py +++ b/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.""" + """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") diff --git a/manage_sql/test.py b/manage_sql/test.py deleted file mode 100644 index a385061..0000000 --- a/manage_sql/test.py +++ /dev/null @@ -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) diff --git a/test.py b/test.py new file mode 100644 index 0000000..708a8a9 --- /dev/null +++ b/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) \ No newline at end of file diff --git a/test_conf.py b/test_conf.py new file mode 100644 index 0000000..d89893b --- /dev/null +++ b/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) \ No newline at end of file