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 hashes)

Uploaded Source

Built Distribution

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