2017-07-18 36 views
0

我使用pypyodbc從ms sql服務器讀取數據。PyPyOdbc正確使用遊標

我不確定遊標是否應該在任何查詢後關閉,或者只是在最後一次,我在文檔中找不到任何東西。

例子:

curs = con.cursor() 

for id in id_list: 
    curs.execute ('Select * from XXX where id = {}'.format (id)) 
    curs.fetchall() 
    # Write the data into the target... 
    # curs.close() ??? 

curs.close() 

這是正確的嗎?

感謝

+0

什麼關閉遊標當你用它做。 –

+0

如果它爲遊標實現了標準PEP(https://www.python.org/dev/peps/pep-0249/),則相關:https://stackoverflow.com/questions/5669878/when-to-close -cursors-使用-MySQLdb的 –

回答

0

with關鍵字是您正在尋找

with sqlite3.connect("db.db") as DB: 
    cursor = DB.cursor() 
    #...perform sql 
# connection automatically closes