python - 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)')
- 蟒蛇:3
- pyodbc:4.0.17
- 操作系统:Windows 10
- 数据库:微软 SQL 服务器
- driver: ['SQL Server', 'SQL Server Native Client 11.0', 'ODBC Driver 13 for SQL Server', 'ODBC Driver 17 for SQL Server']
我尝试使用上述驱动器进行连接。但是我在所有驱动程序选项中都遇到了同样的错误。我在本地机器上运行以连接 Microsoft SQL 服务器。
当我开始阅读有关此问题的信息时,我发现 Microsoft 下的文档指出:
HY090 | 无效的字符串或缓冲区长度 | (DM) BufferLength 中的值小于 0。(参见 SQLSetDescField 中 SQL_DESC_DATA_PTR 字段的描述。)
需要设置一些绑定参数以清除缓冲区,但我认为我没有看到任何可用信息或可在 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)
解决方案
尝试删除“GO”,因为在这种情况下它不应该是必要的。
推荐阅读
- python - Python 多处理加速 I/O 和 Groupby/Sum
- excel - 具有 3 个条件的 SUMPRODUCT,1 行和 2 列的索引匹配
- angular - 如何使我的角度应用程序行为异步
- javascript - 在 React 中将图片从一个组件传递到另一个组件
- javascript - 如何根据卡片的选择将详细信息推送和弹出到javascript中的数组中?
- javascript - 根据从表单中选择的值在屏幕上显示数据
- android - Android:更改样式禁用按钮的文本颜色
- python - 在 Python 中对一系列数据进行分类的最佳方法
- json - 在颤动的下拉按钮上显示选定的数据
- html - 如何使背景图像和背景图像中的元素响应?