【问题标题】:Incorrect syntax near Go with Pypyodbc使用 Pypyodbc 的 Go 附近的语法不正确
【发布时间】:2019-01-02 21:25:25
【问题描述】:

我正在使用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 库相关的已知问题,如果是,我应该在哪里通知开发人员问题。

【问题讨论】:

    标签: sql-server python-3.x pypyodbc


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 2011-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多