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.
Metadata
Release files for psycopg2-utility-functions 0.0.13
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| psycopg2_utility_functions-0.0.13.tar.gz | 5.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| psycopg2_utility_functions-0.0.13-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.2 kB
Release files / psycopg2_utility_functions-0.0.13.tar.gz
| Download URL | psycopg2_utility_functions-0.0.13.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b64bdbd2005b0de4ae785baf4e0d98aa7509f33eb07e2b0b0754b8478bdc501e
|
|
BLAKE2b-256 checksum How to use checksums |
e5d6ad58c5cffb40b842f5c544ef4fd1a9c76efc3c901b698b0ec9bb4c1b6d34
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / psycopg2_utility_functions-0.0.13-py3-none-any.whl
| Download URL | psycopg2_utility_functions-0.0.13-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9d57829f19b54570fa5c6837b270800e30eccb17afbc90ca4a1e5557e28fa5ca
|
|
BLAKE2b-256 checksum How to use checksums |
c50e8b58b4f25ad3509945a1aa42b29e0e73ba82911bd2e46e6ef3e4a422269b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|