## psycopg_dbconn_class
A simple class to handle database connections using psycopg2 (PostgreSQL).
### Development background
When I've started to work with Python + PostgreSQL, I have begun using the pysycopg2
library. However, I do prefer working with classes, and the usual connection/cursor
methods were not very functional for my applications.
Eventually, I decided to structure the objects I needed inside a **DataBaseConnection**
class, and created this repository for further improvements.
### Use
To properly use this class, just download/clone the repository and import the
class in your application.
```python
from psycopg_dbconn_class import DataBaseConnection
```
If you get a **ImportError** message, be sure to install **psycopg2-binary** library as well:
```shell
python -m pip install -r requirements.txt
```
_or_
```shell
python -m pip install pycopg2-binary
```
Then, you can create an object, connect to a server and begin running queries.
By default, parameters for connecting into the server are expected do be in environment variables
('DBN', 'DBU', 'DBK', 'DBH', 'DBP'), but you can also use a json file (_conn.json_). Finally,
you may also enter parameters manually.
```python
from app.psycopg_dbconn_class.src.psycopg_dbconn_class import DataBaseConnection
DB = DataBaseConnection(auto_config=False)
DB.update_config_values(name='db', host='127.0.0.0', port="15432", user="postgres", password="PostgresPass")
DB.connect()
if DB.connected:
# For queries without need of return
DB.run_query('INSERT INTO colum VALUES (NULL)')
DB.run_query('INSERT INTO colum VALUES (%s)', [1])
# For queries that need the return
result = DB.run_read_query('SELECT * FROM table WHERE val = %s', [2], fetch_all=True)
print(result)
DB.close()
```
### Warranty
**psycopg_dbconn_class** is an open-source project. It is distributed in the hope that it will be
useful, but *WITHOUT ANY WARRANTY*; without even the implied warranty of *MERCHANTABILITY*
or *FITNESS FOR A PARTICULAR PURPOSE*. See the GNU Lesser General Public License
version 3 attached for more details.
---
Developed by: [Kleydson Stenio](mailto:kleydson.stenio@gmail.com?Subject=psycopg_dbconn_class_QUESTIONS) @ 2023
Raw data
{
"_id": null,
"home_page": "https://github.com/kstenio/psycopg_dbconn_class",
"name": "psycopg-dbconn-class",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": "",
"keywords": "",
"author": "Kleydson Stenio",
"author_email": "kleydson.stenio@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/1f/09/f0d36b3a1973f7fb2de903bfb356f07d0bc2532168da5d379cf30a12d8a6/psycopg_dbconn_class-0.0.1.tar.gz",
"platform": null,
"description": "## psycopg_dbconn_class\n\nA simple class to handle database connections using psycopg2 (PostgreSQL).\n\n### Development background\n\nWhen I've started to work with Python + PostgreSQL, I have begun using the pysycopg2\nlibrary. However, I do prefer working with classes, and the usual connection/cursor\nmethods were not very functional for my applications.\n\nEventually, I decided to structure the objects I needed inside a **DataBaseConnection**\nclass, and created this repository for further improvements.\n\n\n### Use\n\nTo properly use this class, just download/clone the repository and import the\nclass in your application.\n\n```python\nfrom psycopg_dbconn_class import DataBaseConnection\n```\n\nIf you get a **ImportError** message, be sure to install **psycopg2-binary** library as well:\n\n```shell\npython -m pip install -r requirements.txt \n```\n_or_\n```shell\npython -m pip install pycopg2-binary\n```\n\nThen, you can create an object, connect to a server and begin running queries.\nBy default, parameters for connecting into the server are expected do be in environment variables \n('DBN', 'DBU', 'DBK', 'DBH', 'DBP'), but you can also use a json file (_conn.json_). Finally, \nyou may also enter parameters manually.\n\n```python\nfrom app.psycopg_dbconn_class.src.psycopg_dbconn_class import DataBaseConnection\n\nDB = DataBaseConnection(auto_config=False)\nDB.update_config_values(name='db', host='127.0.0.0', port=\"15432\", user=\"postgres\", password=\"PostgresPass\")\nDB.connect()\n\nif DB.connected:\n\t# For queries without need of return\n\tDB.run_query('INSERT INTO colum VALUES (NULL)')\n\tDB.run_query('INSERT INTO colum VALUES (%s)', [1])\n\t# For queries that need the return\n\tresult = DB.run_read_query('SELECT * FROM table WHERE val = %s', [2], fetch_all=True)\n\tprint(result)\n\nDB.close()\n```\n\n### Warranty\n\n**psycopg_dbconn_class** is an open-source project. It is distributed in the hope that it will be\nuseful, but *WITHOUT ANY WARRANTY*; without even the implied warranty of *MERCHANTABILITY*\nor *FITNESS FOR A PARTICULAR PURPOSE*. See the GNU Lesser General Public License\nversion 3 attached for more details.\n\n---\n\nDeveloped by: [Kleydson Stenio](mailto:kleydson.stenio@gmail.com?Subject=psycopg_dbconn_class_QUESTIONS) @ 2023\n",
"bugtrack_url": null,
"license": "",
"summary": "A simple class to better handle database connections using psycopg2 (PostgreSQL)",
"version": "0.0.1",
"project_urls": {
"Homepage": "https://github.com/kstenio/psycopg_dbconn_class"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ccd4a913b342c0bd60aa34634efa0c5a62a643ddf45c6d7459b0a63c92438d1a",
"md5": "41170175383c3ee4c9c415cad8ed930a",
"sha256": "f48641674bd28fca17d79afad24dc93fd97e98e339c1559ae71de1caf404226d"
},
"downloads": -1,
"filename": "psycopg_dbconn_class-0.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "41170175383c3ee4c9c415cad8ed930a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 20197,
"upload_time": "2023-10-14T20:04:59",
"upload_time_iso_8601": "2023-10-14T20:04:59.725114Z",
"url": "https://files.pythonhosted.org/packages/cc/d4/a913b342c0bd60aa34634efa0c5a62a643ddf45c6d7459b0a63c92438d1a/psycopg_dbconn_class-0.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1f09f0d36b3a1973f7fb2de903bfb356f07d0bc2532168da5d379cf30a12d8a6",
"md5": "4a90f665515b1605c9252683c7d21d45",
"sha256": "11affe7bcb8080c9546fbdcbbeb33520dde260688a1ee7f813caa945bd6f13e3"
},
"downloads": -1,
"filename": "psycopg_dbconn_class-0.0.1.tar.gz",
"has_sig": false,
"md5_digest": "4a90f665515b1605c9252683c7d21d45",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 19532,
"upload_time": "2023-10-14T20:05:02",
"upload_time_iso_8601": "2023-10-14T20:05:02.142023Z",
"url": "https://files.pythonhosted.org/packages/1f/09/f0d36b3a1973f7fb2de903bfb356f07d0bc2532168da5d379cf30a12d8a6/psycopg_dbconn_class-0.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-14 20:05:02",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kstenio",
"github_project": "psycopg_dbconn_class",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "psycopg2-binary",
"specs": [
[
"~=",
"2.9.3"
]
]
},
{
"name": "twine",
"specs": [
[
"==",
"4.0.2"
]
]
}
],
"lcname": "psycopg-dbconn-class"
}