mirror of
https://git.securmail.fr/gizmo/python-module-pgsql.git
synced 2024-12-22 13:54:41 +01:00
First init
This commit is contained in:
commit
df68cc8bfb
25
manage_psql/__init__.py
Executable file
25
manage_psql/__init__.py
Executable file
|
@ -0,0 +1,25 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""Psql connection module."""
|
||||||
|
|
||||||
|
# /*
|
||||||
|
# * ----------------------------------------------------------------------------
|
||||||
|
# * "THE BEER-WARE LICENSE" (Revision 42):
|
||||||
|
# * <boris.tassou@securmail.fr> 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 <boris.tassou@securmail.fr>'
|
||||||
|
__license__ = 'Beerware'
|
||||||
|
|
||||||
|
try:
|
||||||
|
import psycopg2
|
||||||
|
except ImportError:
|
||||||
|
print('Psycopg2 library not found. Books-shelf cannot start.')
|
||||||
|
sys.exit(1)
|
BIN
manage_psql/__init__.pyc
Normal file
BIN
manage_psql/__init__.pyc
Normal file
Binary file not shown.
BIN
manage_psql/__pycache__/__init__.cpython-35.pyc
Normal file
BIN
manage_psql/__pycache__/__init__.cpython-35.pyc
Normal file
Binary file not shown.
BIN
manage_psql/__pycache__/__init__.cpython-36.pyc
Normal file
BIN
manage_psql/__pycache__/__init__.cpython-36.pyc
Normal file
Binary file not shown.
BIN
manage_psql/__pycache__/manage_psql.cpython-35.pyc
Normal file
BIN
manage_psql/__pycache__/manage_psql.cpython-35.pyc
Normal file
Binary file not shown.
BIN
manage_psql/__pycache__/manage_psql.cpython-36.pyc
Normal file
BIN
manage_psql/__pycache__/manage_psql.cpython-36.pyc
Normal file
Binary file not shown.
5
manage_psql/conf.ini
Executable file
5
manage_psql/conf.ini
Executable file
|
@ -0,0 +1,5 @@
|
||||||
|
[psql]
|
||||||
|
host=localhost
|
||||||
|
database=books_shelf
|
||||||
|
user=books_shelf
|
||||||
|
passwd=blabli
|
85
manage_psql/manage_psql.py
Executable file
85
manage_psql/manage_psql.py
Executable file
|
@ -0,0 +1,85 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""Module psql connection."""
|
||||||
|
|
||||||
|
# /*
|
||||||
|
# * ----------------------------------------------------------------------------
|
||||||
|
# * "THE BEER-WARE LICENSE" (Revision 42):
|
||||||
|
# * <boris.tassou@securmail.fr> 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 psycopg2
|
||||||
|
|
||||||
|
|
||||||
|
class HandlePsql:
|
||||||
|
"""Class to handle psql connection."""
|
||||||
|
|
||||||
|
conn = None
|
||||||
|
cur = None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def psql_conn():
|
||||||
|
"""Init psql connection."""
|
||||||
|
# Read conf.ini
|
||||||
|
ini_conf = "/usr/local/Securmail-Administration/config/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')
|
||||||
|
|
||||||
|
# connexion DB
|
||||||
|
# http://initd.org/psycopg/docs/usage.html
|
||||||
|
# http://www.postgresqltutorial.com/postgresql-python/
|
||||||
|
try:
|
||||||
|
global conn
|
||||||
|
conn = psycopg2.connect(host=psql_host, database=psql_db, user=psql_user, password=psql_pwd)
|
||||||
|
conn.cursor()
|
||||||
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
|
print(error)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def psql_select(sql_select):
|
||||||
|
"""Psql select request."""
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute(sql_select)
|
||||||
|
sql_return = cur.fetchall()
|
||||||
|
return sql_return
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def psql_delete(sql_delete):
|
||||||
|
"""Psql delte request."""
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute(sql_delete)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def psql_insert(sql_insert):
|
||||||
|
"""Psql insert request."""
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute(sql_insert)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def psql_update(sql_update):
|
||||||
|
"""Psql insert request."""
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute(sql_update)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def psql_unconn():
|
||||||
|
"""End psql connection."""
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.close()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("Module psql to handle the connection")
|
BIN
manage_psql/manage_psql.pyc
Normal file
BIN
manage_psql/manage_psql.pyc
Normal file
Binary file not shown.
Loading…
Reference in a new issue