【问题标题】:HY090 - Invalid string or buffer length (0)HY090 - 无效的字符串或缓冲区长度 (0)
【发布时间】:2019-02-10 22:10:41
【问题描述】:

我正在尝试使用带有许多插入语句的 .sql 文件向表中插入数据。

声明如下:

INSERT [dbo].[table_name] ([col1], [col2], [col3], [col4], [col5], [col6], [col7], [col8], [col9], [col10], [col11], [col12], [col13], [col14], [col15], [col16], [col17], [col18], [col19], [col20], [col21], [col22], [col23], [col24], [col25]) VALUES (N'01111', N'SOME RANDOM NAME', N'ABCDE', N'Times', N'ABCD', N'0#aa:', N'06', N'SM', N'123 Cerfdty', N'NULL', N'SM', N'NULL', N'NULL', N'000', N'o2:aq', N'wef0', N'000', N'xx:xx', N'xxxxx', N'ZM', NULL, NULL, NULL, NULL, NULL)
GO

每条语句都是单独执行的,然后通过读取文件来提交。

但在完成大约 240 个插入语句后,我收到以下错误:

pyodbc.Error: ('HY090', '[HY090] [Microsoft][ODBC Driver Manager] 无效的字符串或缓冲区长度 (0) (SQLExecDirectW)')

  • Python:3
  • pyodbc: 4.0.17
  • 操作系统:Windows 10
  • 数据库:Microsoft SQL Server
  • 驱动程序: ['SQL 服务器', 'SQL Server Native Client 11.0', '用于 SQL Server 的 ODBC 驱动程序 13', '用于 SQL Server 的 ODBC 驱动程序 17']

我尝试使用上述驱动器进行连接。但是我在所有驱动程序选项中都遇到了同样的错误。我正在本地机器上运行以连接 Microsoft SQL 服务器。

当我开始阅读有关此问题时,我发现 Microsoft 下的文档指出:

https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlbindparameter-function?view=sql-server-2017

HY090 |无效的字符串或缓冲区长度 | (DM) BufferLength 中的值小于 0。(参见 SQLSetDescField 中 SQL_DESC_DATA_PTR 字段的说明。)

https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlsetdescfield-function?view=sql-server-2017

需要设置一些绑定参数以清除缓冲区,但我认为我没有看到任何可用信息或可在 pyodbc 中使用的选项。

我可能明显偏离了这个问题的最初原因。

请帮助我解决这个问题。

try:
    with open(filename,'r') as sqlfile:
        sql_query = ''
        count =0
        for line in sqlfile:
            if 'GO' in line:
                cursor.execute(sql_query)
                sql_query = ''
                conn.commit()
                count = count +1
                print(count)
            elif 'PRINT' in line:
                display = line.split("'")[1]
                print(display)
            else:
                sql_query = sql_query + line 
        sqlfile.close()
except pyodbc.ProgrammingError as error:
    print(error)

【问题讨论】:

  • pyodbc 4.0.17 相当旧。您可以尝试使用 pyodbc 4.0.25 看看效果是否更好?
  • 你好 Gord,我刚刚将 pyodbc 升级到更新版本并尝试再次运行。我遇到了同样的失败
  • 这个问题解决了吗?

标签: python sql-server python-3.x odbc pyodbc


【解决方案1】:

尝试删除“GO”,因为在这种情况下没有必要这样做。

【讨论】:

    猜你喜欢
    • 2014-12-31
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    相关资源
    最近更新 更多