使用 python 中的临时表执行 MS sql 查询序列
Executing sequence of MS sql queries with temp tables in python
目前正在尝试使用 pyodbc
和 pandas
读取 sql 查询序列(来自 MS sql server
)并检查数据帧,在非常序列中的第一个查询:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-95-5eec1638614b> in <module>()
285 for count, q in enumerate(sql_in):
286 print count
--> 287 frame_in = pd.read_sql(q, cnxn)
288 print frame_in.head(n=10)
289
/home/mapr/anaconda2/lib/python2.7/site-packages/pandas/io/sql.pyc in read_sql(sql, con, index_col, coerce_float, params, parse_dates, columns, chunksize)
497 sql, index_col=index_col, params=params,
498 coerce_float=coerce_float, parse_dates=parse_dates,
--> 499 chunksize=chunksize)
500
501 try:
/home/mapr/anaconda2/lib/python2.7/site-packages/pandas/io/sql.pyc in read_query(self, sql, index_col, coerce_float, params, parse_dates, chunksize)
1598 args = _convert_params(sql, params)
1599 cursor = self.execute(*args)
-> 1600 columns = [col_desc[0] for col_desc in cursor.description]
1601
1602 if chunksize is not None:
TypeError: 'NoneType' object is not iterable
相关代码片段如下所示:
sql_in = """
SET NOCOUNT ON;
select <stuff> from <table1> into #<temptable1>
----SPLIT
SET NOCOUNT ON;
select <stuff> from <table2> into #<temptable2>
----SPLIT
SET NOCOUNT ON;
select <stuff> from <table3> into #<temptable3>
----SPLIT
SET NOCOUNT ON;
select <stuff> from <table4> <joined with temptables1-3> into #<temptable4>
""".split('----SPLIT')
for count, q in enumerate(sql_in):
print count
frame_in = pd.read_sql(q, cnxn)
这样做是因为只对最终温度 table 感兴趣(使用 split
因为 here 发布的建议)。
可以通过运行确认pyodbc配置正确:
cnxn = pyodbc.connect(cnxn_str)
cursor = cnxn.cursor()
#Sample select query
print 'Testing db connection...'
cursor.execute("SELECT @@version;")
row = cursor.fetchone()
while row:
print row[0]
row = cursor.fetchone()
并实际获取服务器版本。
知道为什么会发生这种情况以及如何解决吗?谢谢。
我相信这是因为前几个查询没有 return 任何数据。 Pandas read_sql 期待数据返回。您应该只使用 pyodbc execute 函数来 运行 这些查询。
目前正在尝试使用 pyodbc
和 pandas
读取 sql 查询序列(来自 MS sql server
)并检查数据帧,在非常序列中的第一个查询:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-95-5eec1638614b> in <module>()
285 for count, q in enumerate(sql_in):
286 print count
--> 287 frame_in = pd.read_sql(q, cnxn)
288 print frame_in.head(n=10)
289
/home/mapr/anaconda2/lib/python2.7/site-packages/pandas/io/sql.pyc in read_sql(sql, con, index_col, coerce_float, params, parse_dates, columns, chunksize)
497 sql, index_col=index_col, params=params,
498 coerce_float=coerce_float, parse_dates=parse_dates,
--> 499 chunksize=chunksize)
500
501 try:
/home/mapr/anaconda2/lib/python2.7/site-packages/pandas/io/sql.pyc in read_query(self, sql, index_col, coerce_float, params, parse_dates, chunksize)
1598 args = _convert_params(sql, params)
1599 cursor = self.execute(*args)
-> 1600 columns = [col_desc[0] for col_desc in cursor.description]
1601
1602 if chunksize is not None:
TypeError: 'NoneType' object is not iterable
相关代码片段如下所示:
sql_in = """
SET NOCOUNT ON;
select <stuff> from <table1> into #<temptable1>
----SPLIT
SET NOCOUNT ON;
select <stuff> from <table2> into #<temptable2>
----SPLIT
SET NOCOUNT ON;
select <stuff> from <table3> into #<temptable3>
----SPLIT
SET NOCOUNT ON;
select <stuff> from <table4> <joined with temptables1-3> into #<temptable4>
""".split('----SPLIT')
for count, q in enumerate(sql_in):
print count
frame_in = pd.read_sql(q, cnxn)
这样做是因为只对最终温度 table 感兴趣(使用 split
因为 here 发布的建议)。
可以通过运行确认pyodbc配置正确:
cnxn = pyodbc.connect(cnxn_str)
cursor = cnxn.cursor()
#Sample select query
print 'Testing db connection...'
cursor.execute("SELECT @@version;")
row = cursor.fetchone()
while row:
print row[0]
row = cursor.fetchone()
并实际获取服务器版本。
知道为什么会发生这种情况以及如何解决吗?谢谢。
我相信这是因为前几个查询没有 return 任何数据。 Pandas read_sql 期待数据返回。您应该只使用 pyodbc execute 函数来 运行 这些查询。