pad # MySQL Connection Library
The Connection class provides a simple wrapper around `mysql.connector` to manage database connections.
Below is a basic guide on how to use the class along with sample use cases for each function:
## Prerequisites
- Python 3.x
- mysql-connector-python: Install via pip using `pip install mysql-connector-python`
- `.env` file: This should contain your MySQL credentials.
Example:
```
RDS_HOSTNAME=db.dvlp1.circ.zone
# Your database user without @circ.zone
RDS_USERNAME=mydbuser
# Your database password
RDS_PASSWORD=mysecretpassword
# Not mandatory
RDS_DATABASE=mydatabase
LOGZIO_TOKEN=cXNHuVkkffkilnkKzZlWExECRlSKqopE
```
## Usage
**Connection Class:**
```py
from circles_local_database_python.connector import Connector
# Initialization:
connection = Connector.connect("your_database")
# Create a Cursor for Database Connection: #these are examples of usage
cursor = connection.cursor()
cursor.execute("SELECT * FROM my_table")
results = cursor.fetchall()
# Execute a Query.
cursor.execute("INSERT INTO my_table (column1, column2) VALUES (%s, %s)", ("value1", "value2"))
# Commit Changes:
connection.commit()
# Fetch All Rows:
cursor.execute("SELECT * FROM my_table")
rows = cursor.fetchall()
# Fetch One Row:
cursor.execute("SELECT * FROM my_table WHERE column_name='some_value'")
row = cursor.fetchone()
# Get Columns Description:
cursor.description()
# Get Last Inserted ID:
cursor.get_lastrowid()
# Close Connection:
connection.close()
```
# GenericCRUD Class
The GenericCRUD class is a Python utility for simplifying common SQL database operations. It provides an easy-to-use interface to perform basic CRUD (Create, Read, Update, Delete) operations on database tables.
## Usage
You can use either a where condition or an id_column_name and id_column_value to specify which records to select/update/delete.
You can also specify the columns to select (default is all columns).
Here's a simple example of how to use the `GenericCRUD` class:
```python
from circles_local_database_python.generic_crud import GenericCRUD
# Initialize the CRUD object with your schema name and connection (or let it create a default connection).
crud = GenericCRUD('your_database') # if you have a cunnector object, you can pass it as a second argument
# Insert a new record into a table.
data = {'name': 'John', 'age': 30, 'city': 'New York'}
crud.insert(table_name='my_table', data_json=data)
# Select records from a table using id_column_name and id_column_value.
result = crud.select(table_name='my_table', select_clause_value="age, city", id_column_name="name", id_column_value="John", limit=10)
print(result) # (30, 'New York')
# Update records in a table using where condition.
update_data = {'age': 31}
crud.update(table_name='my_table', data_json=update_data, where="name='John'")
# Selecting all columns using where condition.
result = crud.select(table_name='my_table', where="name='John'", limit=10)
print(result) # age is now 31
# select one:
result = crud.select_one(table_name='my_table')
# Delete records from a table using where condition.
crud.delete(table_name='my_table', where="name='John'")
crud.switch_db('your_database2')
# Close the connection when done.
crud.close()
```
# Why to use GenericCRUD?
To reduce duplicate code in all CRUD packages. Have central location with logic i.e. automatic populate fields from UserContext in the future.<br>
Raw data
{
"_id": null,
"home_page": "https://github.com/circles-zone/database-sql-local-python-package",
"name": "database-sql-local",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Circles",
"author_email": "info@circles.life",
"download_url": "https://files.pythonhosted.org/packages/a7/dd/6a14ca45054f57569525644fa115ec77ab79fc90a6393a80bba834eff553/database-sql-local-0.0.111.tar.gz",
"platform": null,
"description": "pad # MySQL Connection Library\n\nThe Connection class provides a simple wrapper around `mysql.connector` to manage database connections. \nBelow is a basic guide on how to use the class along with sample use cases for each function:\n\n## Prerequisites\n\n- Python 3.x\n- mysql-connector-python: Install via pip using `pip install mysql-connector-python`\n- `.env` file: This should contain your MySQL credentials.\nExample:\n\n```\nRDS_HOSTNAME=db.dvlp1.circ.zone\n# Your database user without @circ.zone\nRDS_USERNAME=mydbuser\n# Your database password\nRDS_PASSWORD=mysecretpassword\n# Not mandatory\nRDS_DATABASE=mydatabase\nLOGZIO_TOKEN=cXNHuVkkffkilnkKzZlWExECRlSKqopE\n```\n\n## Usage\n\n**Connection Class:**\n\n```py\nfrom circles_local_database_python.connector import Connector\n\n# Initialization:\nconnection = Connector.connect(\"your_database\")\n\n# Create a Cursor for Database Connection: #these are examples of usage\ncursor = connection.cursor()\ncursor.execute(\"SELECT * FROM my_table\")\nresults = cursor.fetchall()\n\n# Execute a Query.\ncursor.execute(\"INSERT INTO my_table (column1, column2) VALUES (%s, %s)\", (\"value1\", \"value2\"))\n\n# Commit Changes:\nconnection.commit()\n\n# Fetch All Rows:\ncursor.execute(\"SELECT * FROM my_table\")\nrows = cursor.fetchall()\n\n# Fetch One Row:\ncursor.execute(\"SELECT * FROM my_table WHERE column_name='some_value'\")\nrow = cursor.fetchone()\n\n# Get Columns Description:\ncursor.description()\n\n# Get Last Inserted ID:\ncursor.get_lastrowid()\n\n# Close Connection:\nconnection.close()\n```\n\n# GenericCRUD Class\n\nThe GenericCRUD class is a Python utility for simplifying common SQL database operations. It provides an easy-to-use interface to perform basic CRUD (Create, Read, Update, Delete) operations on database tables.\n\n## Usage\n\nYou can use either a where condition or an id_column_name and id_column_value to specify which records to select/update/delete. \nYou can also specify the columns to select (default is all columns).\n\nHere's a simple example of how to use the `GenericCRUD` class:\n\n```python\nfrom circles_local_database_python.generic_crud import GenericCRUD\n\n# Initialize the CRUD object with your schema name and connection (or let it create a default connection).\ncrud = GenericCRUD('your_database') # if you have a cunnector object, you can pass it as a second argument\n\n# Insert a new record into a table.\ndata = {'name': 'John', 'age': 30, 'city': 'New York'}\ncrud.insert(table_name='my_table', data_json=data)\n\n# Select records from a table using id_column_name and id_column_value.\nresult = crud.select(table_name='my_table', select_clause_value=\"age, city\", id_column_name=\"name\", id_column_value=\"John\", limit=10)\nprint(result) # (30, 'New York')\n\n# Update records in a table using where condition.\nupdate_data = {'age': 31}\ncrud.update(table_name='my_table', data_json=update_data, where=\"name='John'\")\n\n# Selecting all columns using where condition.\nresult = crud.select(table_name='my_table', where=\"name='John'\", limit=10)\nprint(result) # age is now 31\n\n# select one:\nresult = crud.select_one(table_name='my_table')\n\n\n# Delete records from a table using where condition.\ncrud.delete(table_name='my_table', where=\"name='John'\")\n\ncrud.switch_db('your_database2')\n\n# Close the connection when done.\ncrud.close()\n```\n# Why to use GenericCRUD?\nTo reduce duplicate code in all CRUD packages. Have central location with logic i.e. automatic populate fields from UserContext in the future.<br>\n",
"bugtrack_url": null,
"license": "",
"summary": "",
"version": "0.0.111",
"project_urls": {
"Homepage": "https://github.com/circles-zone/database-sql-local-python-package"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "32954d992d44908b6b04db76d2df09f50c3fc96704846a33b16270cd1b2d493d",
"md5": "16ee7f8bf21c1f5692b1fd1f2fb2e790",
"sha256": "5e73cc5c3b6812bc948a27a1d67ac2cfa57cec19f00e78d7f9561f4b5fa08acf"
},
"downloads": -1,
"filename": "database_sql_local-0.0.111-py3-none-any.whl",
"has_sig": false,
"md5_digest": "16ee7f8bf21c1f5692b1fd1f2fb2e790",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 9477,
"upload_time": "2023-11-12T06:52:06",
"upload_time_iso_8601": "2023-11-12T06:52:06.501372Z",
"url": "https://files.pythonhosted.org/packages/32/95/4d992d44908b6b04db76d2df09f50c3fc96704846a33b16270cd1b2d493d/database_sql_local-0.0.111-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a7dd6a14ca45054f57569525644fa115ec77ab79fc90a6393a80bba834eff553",
"md5": "2f889230fa52573319975175c188d1db",
"sha256": "7fcd0f145e26c64bcbecb9b13f122bdd4cec9872ffc2e9d312d3f508be45e2a4"
},
"downloads": -1,
"filename": "database-sql-local-0.0.111.tar.gz",
"has_sig": false,
"md5_digest": "2f889230fa52573319975175c188d1db",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 9390,
"upload_time": "2023-11-12T06:52:07",
"upload_time_iso_8601": "2023-11-12T06:52:07.864094Z",
"url": "https://files.pythonhosted.org/packages/a7/dd/6a14ca45054f57569525644fa115ec77ab79fc90a6393a80bba834eff553/database-sql-local-0.0.111.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-12 06:52:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "circles-zone",
"github_project": "database-sql-local-python-package",
"github_not_found": true,
"lcname": "database-sql-local"
}