Looking for the equivalent of dictcursor in flaskext.mysql

后端 未结 2 982
醉话见心
醉话见心 2021-01-06 19:32

I\'ve written a Python Flask app, and initially used MySQLdb to access MySQL. Later I\'ve switched to flaskext.mysql for the same purposes, but now when I use this module I

2条回答
  •  清歌不尽
    2021-01-06 20:10

    I think @alecxe was hinting at this in his last code block, but you can use a DictCursor with the Flask extension as follows:

    As well as the Flask MySQL extension you need the DictCursor

    from flaskext.mysql import MySQL
    from pymysql.cursors import DictCursor
    

    Then simply add the parameter cursorclass=DictCursor when creating the MySQL object:

    mysql = MySQL(cursorclass=DictCursor)
    

    I'm using this in my own Flask app and it seems to be working as expected

    Note: I discovered this solution lurking in a comment in the issue queue in the Github repo for the project here. I do wish the library documentation was more than a few lines long.

提交回复
热议问题