在Django中写sql的方式和pymysql库的写法是类似的。都是用的游标
from django.db import connection #引入数据库连接
cursor=connection.cursor() #使用默认数据库建立游标
cursor.execute(' SELECT b.build_num,b.create_date,b.app_id,b.creater,b.devices,b.state,P.name as plan_name ,S.`name` AS suite_name ,C.name as case_name,A.app_name,A.version,b.device_name from Build as b
LEFT JOIN TestPlans as P ON b.test_plan = P.id
LEFT JOIN TestSuites as S ON b.test_suite =S.id
LEFT JOIN TestCases as C ON b.test_case = C.id
LEFT JOIN Application as A ON b.app_id = A.id
WHERE b.test_plan in (select id from TestPlans where TestPlans.project_id = %d) ORDER BY b.create_date DESC'%(int(project_id)
))
ret=cursor.fetchall()
ret里会拿到游标查询后的数据集合。
个人使用vscode python3.7,使用这个类的时候不会自动加载游标,查看源码connection 的cursor 是等于Any。可能做了底层处理,但直接使用还是能编译通过正常使用的。