# 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:
```bash
pip install psycopg2_utility_functions
```
# Usage
## Connecting to the Database
To establish a connection to a PostgreSQL database:
```commandline
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:
```commandline
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:
```commandline
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:
```commandline
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:
```commandline
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:
```commandline
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.
Raw data
{
"_id": null,
"home_page": "",
"name": "psycopg2-utility-functions",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "python,database,postgresql,psql",
"author": "qxh5696",
"author_email": "qxh5696@g.rit.edu",
"download_url": "https://files.pythonhosted.org/packages/e5/d6/ad58c5cffb40b842f5c544ef4fd1a9c76efc3c901b698b0ec9bb4c1b6d34/psycopg2_utility_functions-0.0.13.tar.gz",
"platform": null,
"description": "# Psycopg2 Utility Functions\n\n\"Psycopg2 Utility Functions\" is a Python package that provides utility functions for basic PostgreSQL database operations using the psycopg2 library.\n\n## Installation\n\nYou can install this package using pip:\n\n```bash\npip install psycopg2_utility_functions\n```\n\n# Usage\n## Connecting to the Database\n\nTo establish a connection to a PostgreSQL database:\n\n```commandline\nfrom psycopg2_utility_functions import connect_to_database\n\nconnection = connect_to_database(database='your_db_name', user='your_db_user', password='your_db_password', host='your_db_host', port='your_db_port')\n```\n\n## Executing Queries\n\nTo execute a SQL query on the connected database:\n\n```commandline\nfrom psycopg2_utility_functions import execute_query\n\nquery = \"CREATE TABLE users (id serial PRIMARY KEY, name varchar, age int);\"\nexecute_query(connection, query)\n```\n\n## Fetching Data\n\nTo fetch data from the database and create objects using a custom class:\n\n```commandline\nfrom psycopg2_utility_functions import fetch_data\n\nclass User:\n def __init__(self, name, age):\n self.name = name\n self.age = age\n\nquery = \"SELECT name, age FROM users WHERE age > %s;\"\nusers = fetch_data(connection, query, User, values=(25,))\n```\n\n## Inserting Data\n\nTo insert a single row of data into a table:\n\n```commandline\nfrom psycopg2_utility_functions import insert_data\n\ndata = {'name': 'Alice', 'age': 30}\ninsert_data(connection, 'users', data)\n```\n\n## Updating Data\n\nTo update rows in a table based on a condition:\n\n```commandline\nfrom psycopg2_utility_functions import update_data\n\ncondition = \"age > %s\"\ndata = {'age': 31}\nupdate_data(connection, 'users', condition, data)\n```\n\n## Deleting Data\n\nTo delete rows from a table based on a condition:\n\n```commandline\nfrom psycopg2_utility_functions import delete_data\n\ncondition = \"age < %s\"\ndelete_data(connection, 'users', condition)\n```\n\n\n## Contributing\n\nFeel free to contribute to this project by opening issues or pull requests on the GitHub repository.\n\n## License\n\nThis project is licensed under the MIT License.\n\n",
"bugtrack_url": null,
"license": "",
"summary": "Utility functions/wrapper for psycopg2 python package",
"version": "0.0.13",
"project_urls": null,
"split_keywords": [
"python",
"database",
"postgresql",
"psql"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c50e8b58b4f25ad3509945a1aa42b29e0e73ba82911bd2e46e6ef3e4a422269b",
"md5": "cb626f254347cd46635a4e5296819c20",
"sha256": "9d57829f19b54570fa5c6837b270800e30eccb17afbc90ca4a1e5557e28fa5ca"
},
"downloads": -1,
"filename": "psycopg2_utility_functions-0.0.13-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cb626f254347cd46635a4e5296819c20",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 6546,
"upload_time": "2023-08-14T22:35:36",
"upload_time_iso_8601": "2023-08-14T22:35:36.880130Z",
"url": "https://files.pythonhosted.org/packages/c5/0e/8b58b4f25ad3509945a1aa42b29e0e73ba82911bd2e46e6ef3e4a422269b/psycopg2_utility_functions-0.0.13-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e5d6ad58c5cffb40b842f5c544ef4fd1a9c76efc3c901b698b0ec9bb4c1b6d34",
"md5": "e5559b08dffafbde945d4fc9a6746fc9",
"sha256": "b64bdbd2005b0de4ae785baf4e0d98aa7509f33eb07e2b0b0754b8478bdc501e"
},
"downloads": -1,
"filename": "psycopg2_utility_functions-0.0.13.tar.gz",
"has_sig": false,
"md5_digest": "e5559b08dffafbde945d4fc9a6746fc9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5619,
"upload_time": "2023-08-14T22:35:38",
"upload_time_iso_8601": "2023-08-14T22:35:38.381872Z",
"url": "https://files.pythonhosted.org/packages/e5/d6/ad58c5cffb40b842f5c544ef4fd1a9c76efc3c901b698b0ec9bb4c1b6d34/psycopg2_utility_functions-0.0.13.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-14 22:35:38",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "psycopg2-utility-functions"
}