def insert_into_bet(data):
len_team_data = int(len(data)/7)
conn=mdb.connect(host='localhost',user='root',passwd='oracle',db='betdb',port=3306)
cur = conn.cursor()
sql = "insert into bet(id,lea,gmd,hos,gue,hos_sco,gue_sco) values(%s,%s,%s,%s,%s,%s,%s)"
for l in range(len_team_data):
for item in data[7*l:7*l+1]:
cur.executemany(sql,data[7*l:7*l+7])
l+=1
cur.close()
conn.commit()
conn.close()
if __name__=='__main__':
# download_live_urls()
now = datetime.datetime.now()
now_date = now.strftime('%Y-%m-%d')
pages_path = '/root/bet/teamUrls/%s' % now_date
for page in os.listdir(pages_path):
page_path = '/root/bet/teamUrls/%s/%s' % (now_date,page)
#问题出在这里!!!
insert_into_bet(from_page_to_data(page_path))
我尝试取消main里面的for循环,然后执行insert_into_bet这个,是可以插入数据库的,但是一旦加入for循环以后,就无法插入数据库了,不知道什么原因,求指点~
for
executemany
仔细看executemany的用法def insert_into_bet(data):
len_team_data = int(len(data)/7)
conn=mdb.connect(host='localhost',user='root',passwd='oracle',db='betdb',port=3306)
cur = conn.cursor()
sql = "insert into bet(id,lea,gmd,hos,gue,hos_sco,gue_sco) values(%s,%s,%s,%s,%s,%s,%s)"
for l in range(len_team_data):
for item in data[7*l:7*l+1]:
cur.executemany(sql,data[7*l:7*l+7])
l+=1
cur.close()
conn.commit()
conn.close()
顺序颠倒了~~
cur.close()
conn.commit()
建议查看日志有没有报错,mysql配置打开global_log,看看sql执行情况
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd19260.html
相关文章 Recommend