Wrapper for mysql.connector.python to get a pool of mysql connections
Project description
flask_mysqlpool
This package allows you to use mysql-connector-pythons pooling feature from flask
Usage
import mysql.connector
from flask import Flask, abort, jsonify
from flask_mysqlpool import MySQLPool
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_PORT'] = 3306
app.config['MYSQL_USER'] = 'test'
app.config['MYSQL_PASS'] = 'test'
app.config['MYSQL_DB'] = 'world_x'
app.config['MYSQL_POOL_NAME'] = 'mysql_pool'
app.config['MYSQL_POOL_SIZE'] = 5
app.config['MYSQL_AUTOCOMMIT'] = True
db = MySQLPool(app)
@app.route('/')
def index():
try:
conn = db.connection.get_connection() # get connection from pool
cursor = conn.cursor(dictionary=True)
cursor.execute("select * from world_x.city limit 10", )
result = cursor.fetchall()
conn.close() # return connection to pool
except mysql.connector.ProgrammingError as err:
print(err)
abort(500)
to_json = [dict(row) for row in result]
return jsonify(to_json)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
flask_mysqlpool-1.0.4.tar.gz
(2.4 kB
view hashes)
Built Distribution
Close
Hashes for flask_mysqlpool-1.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c8574e94ef43be196e2c55402e18dfd983681a03ec485f12b26e5558d7a26f3 |
|
MD5 | 4492e6fbebb6ecaa54d48fd1e29a7f12 |
|
BLAKE2b-256 | 3d09025eb500843873970f17ecdc6b4fd76b4f08863f2295c9b21adb656c4b0e |