我正在使用pypyodbc
库建立与SQL Server 2008 R2数据库的连接,每次尝试执行.sql文件时,都会遇到以下错误:
pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'Go'.")
这是我要执行的sql查询:
Use SL_Site1_App
Go
select emp_num,name, trans_num, job, trans_type
from Hours where trans_type like '1000%' order by trans_date desc
这是我正在使用的python脚本:
import pypyodbc, ExcelFile
def main():
# read the SQL queries externally
queries = ['C:\\Temp\\Ready_to_use_queries\\Connection_sql_python.sql']
for index, query in enumerate(queries):
cursor = initiate_connection_db()
results = retrieve_results_query(cursor, query)
if index == 0:
ExcelFile.write_to_workbook(results)
print("The workbook has been created and data has been inserted.\n")
def initiate_connection_db():
connection_live_db = pypyodbc.connect(driver="{SQL Server}", server="xxx.xxx.xxx.xxx", uid="my-name",
pwd="try-and-guess", Trusted_Connection="No")
connection = connection_live_db.cursor()
return connection
此问题的解决方法是删除Use SL_Site1_App Go
行,但是我想知道这是否是与处理这些行的pypyodbc库有关的已知问题,如果这样,我应该在哪里通知开发人员关于这个问题。
答案 0 :(得分:4)
GO
是sqlcmd
和SSMS使用的批处理分隔符。它不是T-SQL运算符。
考虑到您正在使用应用程序连接到SQL Server,通过添加database="SL_Site1_App"
在连接字符串中声明数据库,然后在您的计算机中删除USE
和GO
语句SQL语句。