- 论坛徽章:
- 0
|
import MySQLdb
cursor = connection.cursor()
cursor.execute(" SELECT * FROM "+tablename+" where id != 0 ")
row=cursor.fetchall()
以上这种是手册上写的,可以用,只不过返回的是tuple,在模板里只能用下标引用,row[0],row[1]这种,很不方便
网上搜到一种方法:
cursor = connection.cursor()
cursor.EXECUTE('''SELECT CAST(AVG( complete_speed ) as decimal(10,2)) as complete_speed,
CAST(AVG( complete_quality ) as decimal(10,2)) as complete_quality ,
CAST(AVG( work_bearing ) as decimal(10,2)) as work_bearing ,
be_marked_user_id , u.first_name , CAST(AVG((complete_speed+complete_quality+work_bearing)/3) as decimal(10,2)) as average
FROM mark_pointresult r left join auth_user u
on u.id=r.be_marked_user_id
GROUP BY be_marked_user_id''')
DESC = cursor.description
DATA = [dict(zip([col[0] FOR col IN DESC], ROW)) FOR ROW IN cursor.fetchall()]
但是这种方法报错,无法使用
请问到底正确的写法是怎样的,就是结果弄成字典,像这样的row['name'],非常感谢!!! |
|