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
Built Distribution
Hashes for psycopg2_utility_functions-0.0.8.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1208e516d051e4f98eb72d7249244328a8a8d22b1b63e45fc1ebe4ff747551c |
|
MD5 | 2f649638709001683ee2df503dde15a8 |
|
BLAKE2b-256 | 3a4a2abf24495308769392141f8aeb6704fd86aa66905c9cf3e8f39e954524d1 |
Hashes for psycopg2_utility_functions-0.0.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1622a360124eed083bcbe9284082cfd7eeb65c631f90147739ebc1d241378f3a |
|
MD5 | a5aad2a620715917c5821d6fd7a41f50 |
|
BLAKE2b-256 | 64ea26c6acbf8f3252496a79b1e60f602051c74611e26c0a71224ee5efddf6f3 |