A simple class to handle database connections using psycopg2 (PostgreSQL)
Project description
psycopg_dbconn_class
A simple class to handle database connections using psycopg2 (PostgreSQL).
Use
To properly use this class, just get it from PyPI.
pip install psycopg-dbconn-class
Alternatively, you can also clone it from the GitHub repository and install manually.
git clone https://github.com/kstenio/psycopg_dbconn_class.git
cd psycopg_dbconn_class
pip install -r requirements.txt
pip install .
Then, you can create an object, connect to a server and begin running queries. By default, parameters for connecting into the server are expected do be in environment variables ('DBN', 'DBU', 'DBK', 'DBH', 'DBP'), but you can also use a json file (conn.json). Finally, you may also enter parameters manually.
from psycopg_dbconn_class import DataBaseConnection
DB = DataBaseConnection(auto_config=False)
DB.update_config_values(name='db', host='127.0.0.0', port="15432", user="postgres", password="PostgresPass")
DB.connect()
if DB.connected:
# For queries without need of return
DB.run_query('INSERT INTO colum VALUES (NULL)')
DB.run_query('INSERT INTO colum VALUES (%s)', [1])
# For queries that need the return
result = DB.run_read_query('SELECT * FROM table WHERE val = %s', [2], fetch_all=True)
print(result)
DB.close()
Development background
When I've started to work with Python + PostgreSQL, I have begun using the pysycopg2 library. However, I do prefer working with classes, and the usual connection/cursor methods were not very functional for my applications.
Eventually, I decided to structure the objects I needed inside a DataBaseConnection class, and created this repository for further improvements.
Warranty
psycopg_dbconn_class is an open-source project. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License version 3 attached for more details.
Developed by: Kleydson Stenio @ 2023
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for psycopg_dbconn_class-0.0.2.tar.gz
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 | 7959deefcd03631c7db6bbfe635957d0ddb51a062b1bcc3f881951d22e7889e6 |
|
| MD5 | c831c5e218f2fe166835f488924d9ba8 |
|
| BLAKE2b-256 | 4c660a41d26108a8e808c2d29616878da7aba5b4a979f7a1c8d12867f327a67c |
Hashes for psycopg_dbconn_class-0.0.2-py3-none-any.whl
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 | 8db37f886d21d44e9f71d3561a7cf95af00e0a8e1fc4a4114625560d108106b9 |
|
| MD5 | 7844074bcc8f5d57207ea7c9e8c36c49 |
|
| BLAKE2b-256 | c09a9f1362fecebcf3d9eeac6fd00ff4af1b65afc6a818d4ccbd77a9299cca39 |