我有一个脚本,它从网页中删除一些数据并将这些数据插入到两个MySQL表中。但每次我的循环在第8次循环后破坏。我的意思是脚本inser 8个新行和脚本用退出代码0完成。select movie_id from movie;
后我在rows
变量中有99行,但只插入8个元素。我的错误在哪里,我该如何解决?
import MySQLdb
db = MySQLdb.connect(host="...", user="...", passwd="...", db="...")
cursor = db.cursor()
def parse_person(actor_id, movie_id):
actor = # ...
sql_insert_actor = """INSERT IGNORE INTO actor (name)
values ("{0}");""".format(actor.name)
sql_insert_tv_actor_mapping = """insert ignore into movie_actor_mapping (actor_id, movie_id) values ("{0}", "{1}");""".format(
cursor.lastrowid,
movie_id
)
try:
cursor.execute(sql_insert_actor)
cursor.execute(sql_insert_tv_actor_mapping)
db.commit()
except:
db.rollback()
def parse_persons(actors_list, movie_id):
for actor in actors_list:
parse_person(actor.actor_id, movie_id)
try:
cursor.execute('select movie_id from movie;')
rows = cursor.fetchall()
for row in rows:
movie_id = row[0]
actors_list = # parse some
parse_persons(actors_list, movie_id)
except:
db.rollback()
db.close()