if __name__ == '__main__':
def result_generator(cursor, batch_size=10):
while True:
results = cursor.fetchmany(batch_size)
if not results:
break
for res in results:
yield res
db = MySQLdb.connect(host="localhost", user="root", passwd="root", db="domains")
# you must create a Cursor object. It will let
# you execute all the queries you need
cursor = db.cursor()
cursor.execute("SELECT domain FROM com ORDER BY id ASC")
for result in result_generator(cursor):
url = "http://www.{0}".format(result[0])
print url
w = Wappalyzer(url)
out = w.analyze()
cursor.execute("""UPDATE com SET frameworks=%s, is_checked=1 WHERE domain=%s""",
(db.escape_string(out.get('frameworks', "")), result[0]))
# disconnect from server
db.commit()
db.close()
我当前的代码仅针对前10行运行。
由于我使用fetchmany
函数,它应该通过选择接下来的10行直到结束来连续运行。
但cursor.execute("""UPDATE ....
会干扰fetchmany cursor.execute("SELECT ...
。
有人可以告诉我防止它的正确方法是什么?
答案 0 :(得分:2)
而不是直接执行UPDATE查询,将它们放在一个字符串中,并在完成SELECT查询结果的迭代后立即运行它们