我有下面的代碼,它從一個表中選擇一些資料,在另一個表中查找相關資料,更新相關表中的資料并從第一個表中洗掉資料。下面使用游標works但不確定是best。我每次都需要像這樣定義一個新的 cursor(x) = db.cursor() 嗎?
db = MySQLdb.connect(host=cred.host, user=cred.user, password=cred.password,
db=cred.db, port=cred.port)
cursor = db.cursor()
cursor.execute("SELECT * FROM tbl_sqs order by timeOfOfferChange ASC limit 200")
for reprice in cursor.fetchall():
#do initial processing of data retreived from tbl_sqs
#select the current value(s) from tbl_inventory_data that are for the same product from the same seller? cursor2 = db.cursor() # prepare a cursor object using cursor() method
cursor2 = db.cursor()
cursor2.execute("SELECT * FROM tbl_inventory_data WHERE `asin`=%s and `user`=%s", (ASIN, SellerId))
db.commit()
for row in cursor2.fetchall(): #iterate over inventory items
cursor3 = db.cursor() # prepare a cursor object using cursor() method#
cursor3.execute("UPDATE tbl_inventory_data SET `…..WHERE `seller-sku`=%s AND `user`=%s"))
db.commit()
cursor4 = db.cursor()
cursor4.execute("DELETE FROM tbl_sqs WHERE MessageId=%s", (message_id)) # delete the message just processed.
db.commit()
uj5u.com熱心網友回復:
您不需要在每次查詢時為資料庫創建游標。最好一次性創建一個游標并使用相同的游標,直到使用完資料庫為止。每個游標創建都會對資料庫產生開銷,并且在大型或繁忙的資料庫中可能會導致一些問題。
另外,在使用完資料庫關閉游標后是好的:
cursor.close()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380028.html
