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['POOL_NAME'] = 'mysql_pool'
app.config['POOL_SIZE'] = 5
app.config['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.2.tar.gz
(2.2 kB
view hashes)
Built Distribution
Close
Hashes for flask_mysqlpool-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cfc1da8c76d4c373aa44bb79c6b50f8415a02fb2f14ab701a6446ed025196e12 |
|
MD5 | 2ccc3af6123dd10e5c6b06d811d29c1a |
|
BLAKE2b-256 | 1ae387aafa17c10799e26e4d5b8885fbd3201c22953478cf5b3bb9218e06ccb3 |