Skip to main content

Utility functions/wrapper for psycopg2 python package

Project description

Psycopg2 Utility Functions

"Psycopg2 Utility Functions" is a Python package that provides utility functions for basic PostgreSQL database operations using the psycopg2 library.

Installation

You can install this package using pip:

pip install psycopg2_utility_functions

Usage

Connecting to the Database

To establish a connection to a PostgreSQL database:

from psycopg2_utility_functions import connect_to_database

connection = connect_to_database(database='your_db_name', user='your_db_user', password='your_db_password', host='your_db_host', port='your_db_port')

Executing Queries

To execute a SQL query on the connected database:

from psycopg2_utility_functions import execute_query

query = "CREATE TABLE users (id serial PRIMARY KEY, name varchar, age int);"
execute_query(connection, query)

Fetching Data

To fetch data from the database and create objects using a custom class:

from psycopg2_utility_functions import fetch_data

class User:
    def __init__(self, name, age):
        self.name = name
        self.age = age

query = "SELECT name, age FROM users WHERE age > %s;"
users = fetch_data(connection, query, User, values=(25,))

Inserting Data

To insert a single row of data into a table:

from psycopg2_utility_functions import insert_data

data = {'name': 'Alice', 'age': 30}
insert_data(connection, 'users', data)

Updating Data

To update rows in a table based on a condition:

from psycopg2_utility_functions import update_data

condition = "age > %s"
data = {'age': 31}
update_data(connection, 'users', condition, data)

Deleting Data

To delete rows from a table based on a condition:

from psycopg2_utility_functions import delete_data

condition = "age < %s"
delete_data(connection, 'users', condition)

Contributing

Feel free to contribute to this project by opening issues or pull requests on the GitHub repository.

License

This project is licensed under the MIT License.

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

psycopg2_utility_functions-0.0.13.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

psycopg2_utility_functions-0.0.13-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file psycopg2_utility_functions-0.0.13.tar.gz.

File metadata

File hashes

Hashes for psycopg2_utility_functions-0.0.13.tar.gz
Algorithm Hash digest
SHA256 b64bdbd2005b0de4ae785baf4e0d98aa7509f33eb07e2b0b0754b8478bdc501e
MD5 e5559b08dffafbde945d4fc9a6746fc9
BLAKE2b-256 e5d6ad58c5cffb40b842f5c544ef4fd1a9c76efc3c901b698b0ec9bb4c1b6d34

See more details on using hashes here.

File details

Details for the file psycopg2_utility_functions-0.0.13-py3-none-any.whl.

File metadata

File hashes

Hashes for psycopg2_utility_functions-0.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 9d57829f19b54570fa5c6837b270800e30eccb17afbc90ca4a1e5557e28fa5ca
MD5 cb626f254347cd46635a4e5296819c20
BLAKE2b-256 c50e8b58b4f25ad3509945a1aa42b29e0e73ba82911bd2e46e6ef3e4a422269b

See more details on using hashes here.

Supported by

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