我试图在MySQL数据库上进行多次插入,如下所示:
p = 1
orglist = buildjson(buildorgs(p, p))
while (orglist is not None):
for org in orglist:
sid = org['sid']
try:
sql = "INSERT INTO `Orgs` (`sid`) VALUES (\"{0}\");".format(sid)
cursor.execute(sql)
print("Added {0}".format(org['title']))
except Exception as bug:
print(bug)
conn.commit()
conn.close()
p += 1
orglist = buildjson(buildorgs(p, p))
但是我不断收到一堆2055: Lost connection to MySQL server at 'localhost:3306', system error: 9 Bad file descriptor
如何一次正确地执行多次插入,因此每次插入后我都不必提交。另外,我可以在while循环之后只进行conn.close()
还是将它保持在最佳状态?
答案 0 :(得分:2)
这可能与this question和/或this question有关。从您可能尝试的那些问题的答案中得出几个想法:
cursor.close()
之前conn.close()
;我不知道您是否应该在conn.commit()
之前或之后关闭游标,所以请尝试两者。)< / LI>