首页 > 解决方案 > HY090 - 无效的字符串或缓冲区长度 (0)

问题描述

我正在尝试使用带有许多插入语句的 .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)')

我尝试使用上述驱动器进行连接。但是我在所有驱动程序选项中都遇到了同样的错误。我在本地机器上运行以连接 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)

标签: pythonsql-serverpython-3.xodbcpyodbc

解决方案


尝试删除“GO”,因为在这种情况下它不应该是必要的。


推荐阅读