使用 Pypyodbc 时 Go 附近的语法不正确

aba*_*sta 1 sql-server python-3.x pypyodbc

我正在使用该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
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的 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
Run Code Online (Sandbox Code Playgroud)

此问题的解决方法是删除该Use SL_Site1_App Go行,但我想知道这是否是与 pypyodbc 库处理这些行相关的已知问题,如果是,我应该在哪里通知开发人员有关此问题的信息。

use*_*983 5

GOsqlcmd是SSMS使用的批次分隔符。它不是 T-SQL 运算符。

考虑到您正在使用应用程序连接到 SQL Server,请通过添加 来在连接字符串中声明您的数据库,database="SL_Site1_App"然后删除SQL 语句中的USE和语句。GO