在flaskext.mysql中寻找dictcursor的等效项 [英] Looking for the equivalent of dictcursor in flaskext.mysql

查看:101
本文介绍了在flaskext.mysql中寻找dictcursor的等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个Python Flask应用程序,最初使用MySQLdb访问MySQL.后来出于相同的目的,我切换到flaskext.mysql,但是现在当我使用此模块时,我看不到如何获取字典结构的游标.

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 cannot see how to get a dictionary structured cursor.

当我使用MySQLdb模块时,我正在使用以下行打开基于字典的游标-

When I using the MySQLdb module I was using the following line to open a dictionary based cursor -

import MySQLdb as mdb
con = mdb.connect('localhost','root','root','transport')
    with con:
        cur = con.cursor(mdb.cursors.DictCursor)

现在我正在尝试使用flaskext.mysql进行相同的操作,我的当前代码如下所示-

Now I'm trying to do the same with flaskext.mysql, my currect code looks like this -

from flaskext.mysql import MySQL
cur = mysql.get_db().cursor()

为了获得相同类型的游标,我应该向游标对象提供什么?

What should I feed the cursor object in order to get the same type of cursor?

推荐答案

mysql.get_db() 会进入连接"对象,您可以执行以下操作:

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

import MySQLdb as mdb

cur = mysql.get_db().cursor(mdb.cursors.DictCursor)

或者,您也可以在初始化扩展名时设置默认的cursorclass:

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

mysql = MySQL(cursorclass=mdb.cursors.DictCursor)

这篇关于在flaskext.mysql中寻找dictcursor的等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆