https://github.com/aleasoluciones/infmysql
🗄️ MySQL infrastructure (wrapper for mysqlclient)
https://github.com/aleasoluciones/infmysql
mysql mysqlclient mysqldb python3 wrapper
Last synced: 4 months ago
JSON representation
🗄️ MySQL infrastructure (wrapper for mysqlclient)
- Host: GitHub
- URL: https://github.com/aleasoluciones/infmysql
- Owner: aleasoluciones
- Created: 2021-03-23T07:43:34.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2025-11-25T06:19:50.000Z (6 months ago)
- Last Synced: 2025-11-28T14:28:07.189Z (6 months ago)
- Topics: mysql, mysqlclient, mysqldb, python3, wrapper
- Language: Python
- Homepage:
- Size: 48.8 KB
- Stars: 0
- Watchers: 10
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# infmysql
[](https://github.com/aleasoluciones/infmysql/actions/workflows/ci.yml)

Wrapper for the [mysqlclient](https://mysqlclient.readthedocs.io) library using Python 3.
## Development
### Setup
Create a virtual environment, install dependencies and load environment variables.
```python
mkvirtualenv infmysql -p $(which python3)
dev/setup_venv.sh
source dev/env_develop
```
Run a MySQL Docker container.
```python
dev/start_local_dependencies.sh
```
### Running tests, linter & formatter and configure git hooks
Note that project uses Alea's [pydevlib](https://github.com/aleasoluciones/pydevlib), so take a look at its README to see the available commands.
## infmysql client API
Below is described the public API that this library provides.
The client can be initialized using the factory with a database URI and an optional parameter which determines if we want to use a dict cursor (false by default).
> mysql_client = factory.**mysql_client**(*database_uri*, *use_dict_cursor=False*)
### execute()
Executes a SQL query and returns the result. Passing parameters is possible by using `%s` placeholders in the SQL query, and passing a sequence of values as the second argument of the function.
> mysql_client.**execute**(*query*, *params*)
➡️ Parameters
- **query**: `str`
- **params** (optional): `tuple`. Defaults to `None`.
⬅️ Returns a tuple of tuples or dictionaries, each containing a row of results.
`tuple>`
💥 Throws the same exceptions than the mysqlclient library.
#### Usage example
```python
from infmysql import factory
mysql_client = factory.mysql_client('mysql://username:password@host:port/databasename')
sql_query = 'SELECT (name, surname, age) FROM users WHERE age < %s AND active = %s;'
params = (30, True, )
result = mysql_client.execute(sql_query, params)
# (
# ('Ann', 'White', 18, ),
# ('Axel', 'Schwarz', 21, ),
# ('Camille', 'Rouge', '27', ),
# )
```
#### Usage example with dict cursor
```python
from infmysql import factory
mysql_client = factory.mysql_client('mysql://username:password@host:port/databasename', use_dict_cursor=True)
sql_query = 'SELECT (name, surname, age) FROM users WHERE age < %s AND active = %s;'
params = (30, True, )
result = mysql_client.execute(sql_query, params)
# (
# {'name': 'Ann', 'surname': 'White', 'age': 18},
# {'name': 'Axel', 'surname': 'Schwarz', 'age': 21},
# {'name': 'Camille', 'surname': 'Rouge', 'age': 27},
# )
```