Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psycopg_dbconn_class-0.0.2.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

psycopg_dbconn_class-0.0.2-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file psycopg_dbconn_class-0.0.2.tar.gz.

File metadata

  • Download URL: psycopg_dbconn_class-0.0.2.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for psycopg_dbconn_class-0.0.2.tar.gz
Algorithm Hash digest
SHA256 7959deefcd03631c7db6bbfe635957d0ddb51a062b1bcc3f881951d22e7889e6
MD5 c831c5e218f2fe166835f488924d9ba8
BLAKE2b-256 4c660a41d26108a8e808c2d29616878da7aba5b4a979f7a1c8d12867f327a67c

See more details on using hashes here.

File details

Details for the file psycopg_dbconn_class-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for psycopg_dbconn_class-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8db37f886d21d44e9f71d3561a7cf95af00e0a8e1fc4a4114625560d108106b9
MD5 7844074bcc8f5d57207ea7c9e8c36c49
BLAKE2b-256 c09a9f1362fecebcf3d9eeac6fd00ff4af1b65afc6a818d4ccbd77a9299cca39

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page