Skip to content Skip to sidebar Skip to footer

Typeerror: Cursor() Got An Unexpected Keyword Argument 'dictionary' Using Flaskext.mysql

I'm using flask framework and MySQL database connector. I did the below: from flaskext.mysql import MySQL mysql = MySQL() ## app.config database configuration code mysql.init_app(

Solution 1:

You can follow this answer: https://stackoverflow.com/a/41388992/3129414

Existing code modification:

  • use DictCursor in MySQL initialization
  • remove dictionary=True from cursor
  • use json package to convert dictionary items to json format

app.py:

import json
from flask import Flask
from flaskext.mysql import MySQL
from pymysql.cursors import DictCursor

app = Flask(__name__)
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = '123'
app.config['MYSQL_DATABASE_DB'] = 'practice_db'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql = MySQL(cursorclass=DictCursor)
mysql.init_app(app)
conn = mysql.connect()

@app.route("/", methods=['GET'])defhome():
    email_address = 'dummy@example.com'
    cursor = conn.cursor();
    cursor.execute(
            'SELECT * FROM user_table WHERE email = (%s)', (email_address,)
        )
    users = cursor.fetchall()
    return json.dumps(users)

app.run(debug=True)

Screenshot:

dictionary output using flaskext

Post a Comment for "Typeerror: Cursor() Got An Unexpected Keyword Argument 'dictionary' Using Flaskext.mysql"