Looking for the equivalent of dictcursor in flaskext.mysql

后端 未结 2 961
醉话见心
醉话见心 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.

    0 讨论(0)
  • 2021-01-06 20:16

    mysql.get_db() would result into your "connection" object, you can do:

    import MySQLdb as mdb
    
    cur = mysql.get_db().cursor(mdb.cursors.DictCursor)
    

    Or, you can also set the default cursorclass when initializing the extension:

    mysql = MySQL(cursorclass=mdb.cursors.DictCursor)
    
    0 讨论(0)
提交回复
热议问题