简体   繁体   中英

Python - How to prevent MySQLdb cursor update interfering with fetchmany?

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()

My current code runs only for the first 10 rows.

Since i'm using fetchmany function, its supposed to run continuously by selecting the next 10 rows until the end.

But the cursor.execute("""UPDATE .... interfere with the fetchmany cursor.execute("SELECT ... .

Can someone tell me what is the proper way to prevent it?

而不是直接执行UPDATE查询,将它们放在一个字符串中,并在完成SELECT查询的结果迭代后立即运行它们

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM