commit 44fd7d1a09380bab848837e4a0de5ef37acdbb57 Author: Boris Tassou Date: Tue Dec 3 11:10:02 2019 +0100 First init diff --git a/manage_sql/.DS_Store b/manage_sql/.DS_Store new file mode 100644 index 0000000..38734ca Binary files /dev/null and b/manage_sql/.DS_Store differ diff --git a/manage_sql/__init__.py b/manage_sql/__init__.py new file mode 100755 index 0000000..8d9a827 --- /dev/null +++ b/manage_sql/__init__.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +"""Sql connection module.""" + +# /* +# * ---------------------------------------------------------------------------- +# * "THE BEER-WARE LICENSE" (Revision 42): +# * wrote this file. As long as you retain this notice you +# * can do whatever you want with this stuff. If we meet some day, and you think +# * this stuff is worth it, you can buy me a beer in return Boris Tassou +# * ---------------------------------------------------------------------------- +# */ + +import sys + +# Global name +__version__ = '1.0' +__author__ = 'Boris Tassou ' +__license__ = 'Beerware' + +try: + import MySQLdb +except ImportError: + print('MySQLdb library not found') + sys.exit(1) diff --git a/manage_sql/__init__.pyc b/manage_sql/__init__.pyc new file mode 100644 index 0000000..3f7fe3c Binary files /dev/null and b/manage_sql/__init__.pyc differ diff --git a/manage_sql/__pycache__/__init__.cpython-35.pyc b/manage_sql/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..330800e Binary files /dev/null and b/manage_sql/__pycache__/__init__.cpython-35.pyc differ diff --git a/manage_sql/__pycache__/__init__.cpython-36.pyc b/manage_sql/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..38d6fb5 Binary files /dev/null and b/manage_sql/__pycache__/__init__.cpython-36.pyc differ diff --git a/manage_sql/__pycache__/manage_psql.cpython-35.pyc b/manage_sql/__pycache__/manage_psql.cpython-35.pyc new file mode 100644 index 0000000..70ad171 Binary files /dev/null and b/manage_sql/__pycache__/manage_psql.cpython-35.pyc differ diff --git a/manage_sql/__pycache__/manage_sql.cpython-36.pyc b/manage_sql/__pycache__/manage_sql.cpython-36.pyc new file mode 100644 index 0000000..43efb6f Binary files /dev/null and b/manage_sql/__pycache__/manage_sql.cpython-36.pyc differ diff --git a/manage_sql/conf.ini b/manage_sql/conf.ini new file mode 100755 index 0000000..7554ff5 --- /dev/null +++ b/manage_sql/conf.ini @@ -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 new file mode 100755 index 0000000..815510b --- /dev/null +++ b/manage_sql/manage_sql.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +"""Module sql connection.""" + +# /* +# * ---------------------------------------------------------------------------- +# * "THE BEER-WARE LICENSE" (Revision 42): +# * wrote this file. As long as you retain this notice you +# * can do whatever you want with this stuff. If we meet some day, and you think +# * this stuff is worth it, you can buy me a beer in return Boris Tassou +# * ---------------------------------------------------------------------------- +# */ + +from configparser import SafeConfigParser +import MySQLdb + + +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) + + # 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) + cur = conn.cursor() + except (Exception, MySQLdb.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 + + def sql_delete(self, sql_delete): + """Sql delte request.""" + # global cur + cur = conn.cursor() + cur.execute(sql_delete) + 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() + +if __name__ == "__main__": + print("Module sql to handle the connection") diff --git a/manage_sql/manage_sql.pyc b/manage_sql/manage_sql.pyc new file mode 100644 index 0000000..7b88a70 Binary files /dev/null and b/manage_sql/manage_sql.pyc differ diff --git a/manage_sql/test.py b/manage_sql/test.py new file mode 100644 index 0000000..a385061 --- /dev/null +++ b/manage_sql/test.py @@ -0,0 +1,21 @@ +#!/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)