성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Roll A Lisp In C - Reading ; https...
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>_mysql - Commands out of sync; you can't run this command now</h1> <p> 다음의 코드를 수행하면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > # MySQLdb._mysql # <a target='tab' href='https://mysqlclient.readthedocs.io/user_guide.html#some-mysql-examples'>https://mysqlclient.readthedocs.io/user_guide.html#some-mysql-examples</a> def mysqlclient_raw2(request): from MySQLdb import _mysql mysql_ip = util.get_mysql_ip() con = _mysql.connect(mysql_ip, "...", "...", "...") <span style='color: blue; font-weight: bold'>con.query("SELECT SLEEP(0)") con.commit()</span> con.close() return render(request, 'bbs/mysqlclient_raw.html', {'result_set': None}) </pre> <br /> (필요없는 호출이긴 하지만) con.commit()에서 이런 예외가 발생합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Traceback (most recent call last): File "/home/kevin/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/home/kevin/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/mnt/d/workshop2/python-agent/testprj/myapp/./bbs/views.py", line 346, in mysqlclient_raw con.commit() MySQLdb.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") </pre> <br /> 관련해서 검색해 보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > B.3.2.12 Commands out of sync ; <a target='tab' href='https://dev.mysql.com/doc/refman/8.0/en/commands-out-of-sync.html'>https://dev.mysql.com/doc/refman/8.0/en/commands-out-of-sync.html</a> </pre> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in betwee<br /> </div><br /> <br /> con.query("SELECT SLEEP(0)") 코드 수행으로 resultset이 열려 있게 되고, 그런 와중에 con.commit()으로 인해 수행되는 또 다른 MySql 쿼리로 인해 문제가 되는 것 같습니다.<br /> <br /> 따라서, 이 문제를 해결하려면 commit 전에 ("SELECT"를 동반한 쿼리인 경우라면) 반드시 store_result를 호출해야 합니다. (혹은, commit을 호출하지 않으면 됩니다.)<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > con = _mysql.connect(mysql_ip, "...", "...", "...") con.query("SELECT SLEEP(0)") <span style='color: blue; font-weight: bold'>con.store_result()</span> con.commit() </pre> <br /> 반면에 use_result를 쓰면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > con.query("SELECT SLEEP(0)") r = con.use_result() con.commit() # 예외 발생: MySQLdb.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") </pre> <br /> 다시 예외가 발생하는데, "<a target='tab' href='https://dev.mysql.com/doc/refman/8.0/en/commands-out-of-sync.html'>B.3.2.12 Commands out of sync</a>" 문서에서 설명했듯이 use_result/free_result를 쌍으로 호출해야 하지만 웬일인지 MySQLdb._mysql에는 해당 함수가 제공되지 않습니다. (MySQL측의 <a target='tab' href='https://dev.mysql.com/doc/connector-python/en/connector-python-api-cext-free-result.html'>connector에는 free_result 함수가 제공</a>됩니다.)<br /> <br /> 그렇다면 use_result와 store_result 간에는 어떤 차이점이 있을까요?<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > mysqli_store_result() vs. mysqli_use_result() ; <a target='tab' href='https://stackoverflow.com/questions/9876730/mysqli-store-result-vs-mysqli-use-result'>https://stackoverflow.com/questions/9876730/mysqli-store-result-vs-mysqli-use-result</a> </pre> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> mysqli::store_result() will fetch the whole resultset from the MySQL server while mysqli::use_result() will fetch the rows one by one.<br /> </div><br /> <br /> 설명에 따르면 store_result의 경우 resultset을 전부 순회한 것이 되고, use_result의 경우에는 그렇지 않은 유형입니다. 따라서, 다음과 같이 직접 순회를 마치면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > con.query("SELECT SLEEP(0)") r = con.use_result() r.fetch_row() # 2번을 호출 r.fetch_row() con.commit() # 정상 동작 </pre> <br /> 또는, while 문으로 돌려주면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > con.query("SELECT SLEEP(0)") r = con.use_result() while r.fetch_row(): pass con.commit() # 정상 동작 </pre> <br /> commit 호출 시 예외가 발생하지 않습니다. 참고로, 다른 의견을 보면 그 둘 간의 차이점에는 buffer가 관여한다고 합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > use_result returns an unbuffered result. store_result returns a buffered result. </pre> <br /> 그렇다면 store_result의 경우에도 버퍼를 초과하는 select인 경우라면 문제가 된다는 것일까요? 딱히 버퍼의 용량을 알아내는 것도 없으니 테스트가 애매합니다.<br /> <br /> 정리해 보면, 결국 제가 _mysql의 사용법을 제대로 익히지 않았다고 볼 수 있습니다. ^^<br /> </p><br /> <br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1581
(왼쪽의 숫자를 입력해야 합니다.)