1
파이썬 2.7.13의 파일에서 SQL 쿼리를 실행하고 결과 집합을 표시하는 동안 다음 오류가 발생합니다. 파일의 SQL 문은 테이블의 count (*)처럼 단순하지만이 논리가 작동하면 복잡한 쿼리로 바꿔야합니다.Python - 파일에서 다중 SQL 쿼리 실행
오류
Info : (7,)
Traceback (most recent call last):
File "SQLserver_loop.py", line 19, in <module>
fields = c.fetchall()
File "pymssql.pyx", line 542, in pymssql.Cursor.fetchall (pymssql.c:9352)
pymssql.OperationalError: Statement not executed or executed statement has no re
sultset
파이썬 스크립트 :
import pymssql
conn = pymssql.connect(
host=r'name',
user=r'user',
password='credential',
database='Test')
c = conn.cursor()
fd = open('ZooDatabase.sql', 'r') # Open and read the file as a single buffer
sqlFile = fd.read()
fd.close()
sqlCommands = sqlFile.split(';') # all SQL commands (split on ';')
for command in sqlCommands: # Execute every command from the input file
c.execute(command)
fields = c.fetchall()
for row in fields:
print "Info : %s " % str(row)
c.close()
conn.close()
오류 메시지
**SQL File - ZooDatabase.sql**
select count(*) from emp2;
select count(*) from emp1;
**Error Log with SQL print statement output:**
C:\Python27\pycode>python SQLserver_loop.py
SELECT count(*) FROM emp2
Info : (7,)
SELECT count(*) FROM emp1
Info : (7,)
Traceback (most recent call last):
File "SQLserver_loop.py", line 20, in <module>
fields = c.fetchall()
File "pymssql.pyx", line 542, in pymssql.Cursor.fetchall (pymssql.c:9352)
pymssql.OperationalError: Statement not executed or executed statement has no re
sultset
파일의 검색어 형식을 확인하십시오. 질문에 추가하십시오. – shadow0359
코드를 실행하면 쿼리가 표시되므로 문제를 일으키는 쿼리를 알 수 있으므로 질문을 [편집]하여 해당 쿼리의 모양을 표시하십시오. –
은 질문에서 SQL file & print statement의 요청 된 정보를 추가했습니다. – Rajeev