반응형
MySQLdb를 사용하여 Python을 통해 MySQL-Server에 연결하는 경우. 다음과 같이 연결
과 커서
를 만듭니다.
connection = MySQLdb.connect(...)
cursor = connection.cursor()
# process
MySQL 처리가 완료되면 연결
을 닫아야합니다. 이제 궁금합니다. 다음을 수행하여 연결
을 종료하는 것으로 충분합니까?
connection.close()
아니면 먼저 커서
를 닫은 다음 연결
을 닫아야합니까? 이렇게 :
cursor.close()
connection.close()
해결 방법
with
를 사용하면 이전 들여 쓰기 수준으로 돌아 가면 닫힐 임시 커서를 만들 수 있습니다.
from contextlib import closing
with closing( connection.cursor() ) as cursor:
(indented) use the cursor
(non-indented) cursor is closed.
connection.close()
참조 페이지 https://stackoverflow.com/questions/5504340
반응형
'MySql' 카테고리의 다른 글
MySQL 예외 : 먼저 닫아야하는이 연결과 연결된 열린 DataReader가 이미 있습니다. (0) | 2020.10.13 |
---|---|
MySQL What column data type should I use for storing large amounts of text or html (0) | 2020.10.13 |
MySQL Where does WAMP server store database files (0) | 2020.10.13 |
MySQL PHP를 사용하여 MySQL에서 특정 시간 범위의 레코드를 어떻게 선택합니까? (0) | 2020.10.13 |
MySQL Mac OS X에 MySQLdb 설치 (0) | 2020.10.13 |
댓글