The*_*net 5 python mysql flask
我正在尝试使用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()
Run Code Online (Sandbox Code Playgroud)
错误:
user = g.db.cursor().execute(sql).fetchall()
AttributeError: 'long' object has no attribute 'fetchall'
Run Code Online (Sandbox Code Playgroud)
为什么不返回结果集?
另外,我可以很好地执行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
Run Code Online (Sandbox Code Playgroud)
Jam*_*dge 10
您正试图在结果上调用一个方法Cursor.execute,DB-API规范说这是未定义的(您正在使用的实现似乎返回一个整数).相反,您想要调用fetchall游标对象.就像是:
cursor = g.db.cursor()
cursor.execute(sql)
user = cursor.fetchall()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9139 次 |
| 最近记录: |