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 38d6fb5..21d5920 100644 Binary files a/manage_sql/__pycache__/__init__.cpython-36.pyc and b/manage_sql/__pycache__/__init__.cpython-36.pyc differ diff --git a/manage_sql/__pycache__/manage_sql.cpython-36.pyc b/manage_sql/__pycache__/manage_sql.cpython-36.pyc index 43efb6f..9eebd31 100644 Binary files a/manage_sql/__pycache__/manage_sql.cpython-36.pyc and b/manage_sql/__pycache__/manage_sql.cpython-36.pyc differ 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