gpt4 book ai didi

python - 错误: 'NoneType' object is not iterable when read_sql

转载 作者:行者123 更新时间:2023-12-01 00:20:34 26 4
gpt4 key购买 nike

在互联网上寻找解决方案。

这是代码:

import pyodbc
import pandas as pd
conn = pyodbc.connect("Driver={SQL Server};"
"Server=Server;"
"Trusted_Connection=yes;")
cursor = conn.cursor()
query = """
SET
nocount
ON;
USE database;
SELECT
*
FROM
(
SELECT
db.column1 AS col1,
db.column2 AS col2,
db.column3 AS col3,
CASE
WHEN
db.column4 IS NULL
THEN
0
ELSE
1
END
AS col4,
db.column5 AS col5
FROM
TABLE AS tab
INNER JOIN
data as db
ON ( operator )
)
AS sivola
WHERE
sivola.col2 > x
ORDER BY
sivola.col2 ASC
"""
df = pd.read_sql(query, conn)

虽然我正在使用SET nocount ON; (正如许多帖子中所建议的那样),我仍然收到错误 TypeError: 'NoneType' object is not iterable

有什么可行的解决方案吗?

编辑:完整追溯

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-56-f01b80801200> in <module>()
82 logging.error('Starting query from BL \n')
83
---> 84 df = pd.read_sql(query, conn)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\sql.py in read_sql(sql, con, index_col, coerce_float, params, parse_dates, columns, chunksize)
379 sql, index_col=index_col, params=params,
380 coerce_float=coerce_float, parse_dates=parse_dates,
--> 381 chunksize=chunksize)
382
383 try:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\sql.py in read_query(self, sql, index_col, coerce_float, params, parse_dates, chunksize)
1434 args = _convert_params(sql, params)
1435 cursor = self.execute(*args)
-> 1436 columns = [col_desc[0] for col_desc in cursor.description]
1437
1438 if chunksize is not None:

TypeError: 'NoneType' object is not iterable

最佳答案

你可以删除设置
没有计数
在;
使用数据库;

我认为从连接中获取结果的方式是不正确的。要么使用光标并执行任务而不获取任何内容,要么像您一样仅使用连接中的选择来获取内容,但没有这些行。

关于python - 错误: 'NoneType' object is not iterable when read_sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58995873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com