問題描述
測試中,需要用不同的數據去請求接口,然後去對比數據庫確認後端計算值是否正確。 在對比數據庫時,發現,如果在頭部就初始化DB db = pymysql.connect(host='xxx', port=3306, user='xxx', passwd='xxx', db='vcs', charset='utf8')
在循環體中創建遊標cursor = db.cursor()
之後,第二次循環時,遊標取值爲空
解決方案
在循環中初始化BD 每次循環都重新創建遊標對象
for c in id_card:
db = pymysql.connect(host='xxx', port=3306, user='xxx', passwd='xxx', db='vcs', charset='utf8')
cursor = db.cursor()
cursor.execute(sql1)
dbre = cursor.fetchall()
解決
總結
需要好好理解面向對象的思想