python执行mysql插入时间超长
和你的代码一样,为什么我执行mysql插入时花费的时间
[Finished in 164.8s]
和你的代码一样,为什么我执行mysql插入时花费的时间
[Finished in 164.8s]
2016-09-05
values = [] # 输出所有的词条对应的名称和URL属性 for url in listUrls: #过滤.jpg或.JPG结尾的URL if not re.search("\.(jpg|JPG)$",url["href"]): #输出URL的文字和对应的链接 print(url.get_text(),"<----->","https://en.wikipedia.org"+url["href"]) values.append((url.get_text(),"https://en.wikipedia.org"+url["href"])) try: connection = pymysql.connect(host="localhost",user="root",password="root",db="wikiurl",charset="utf8mb4") with connection.cursor() as cursor: sql = "insert into `urls`(`urlname`,`urlhref`)values(%s,%s)" cursor.executemany(sql,values) connection.commit() except: info = sys.exc_info() print(info) print(info[0]) print(info[1]) finally: connection.close()
上面代码我换了思路,把mysql操作放在最后来做,同时调用cursor.executemany, 一次可以插入多条记录
举报