加入收藏 | 设为首页 | 会员中心 | 我要投稿 常州站长网 (https://www.0519zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

python – AttributeError:’long’对象没有属性’fetchall’

发布时间:2021-05-22 14:51:13 所属栏目:MySql教程 来源:网络整理
导读:我正在尝试使用mysql-flask python扩展来执行一些sql.由于某种原因,下面的代码总是返回long. stringify = lambda x : '"' + x + '"'if request.method == 'POST': sql = "select * from users where username = " + stringify(request.form['username']) u

我正在尝试使用mysql-flask python扩展来执行一些sql.由于某种原因,下面的代码总是返回long.

stringify = lambda x : '"' + x + '"'
if request.method == 'POST':
        sql = "select * from users where username = " + stringify(request.form['username'])
        user = g.db.cursor().execute(sql).fetchall()

错误:

 user = g.db.cursor().execute(sql).fetchall()
AttributeError: 'long' object has no attribute 'fetchall'

为什么不返回结果集?

另外,我可以很好地执行insert语句.

修复(答案):

def get_data(g,sql):
    cursor = g.db.cursor()
    cursor.execute(sql)
    data = [dict((cursor.description[idx][0],value) for idx,value in enumerate(row)) for row in cursor.fetchall()]
    return data
最佳答案 你试图在Cursor.execute的结果上调用一个方法,DB-API specification所说的是未定义的(你正在使用的实现似乎返回一个整数).相反,您希望在游标对象上调用fetchall.就像是:

cursor = g.db.cursor()
cursor.execute(sql)
user = cursor.fetchall()

(编辑:常州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读