NoneType object is not an iterable error in pandas

I am trying to extract some data from a stored proc on sql server using python.

Here is my code:

import datetime as dt
import pyodbc
import pandas as pd

conn = pyodbc.connect('Trusted_Connection=yes', driver = '{SQL Server Native client 11.0}',server = '*****, database = '**')

pd.read_sql("EXEC ******** '20140528'",conn)

I get the error: TypeError: object "NoneType" is not iterable

I suspect this is because I have a cell in the sql table with a NULL value, but I'm not sure if this is the true reason I get the error. I have run many sql statements using the same code without any errors.

Here's the trace:

In[39]: pd.read_sql("EXEC [dbo].[] '20140528'",conn)
Traceback (most recent call last):
  File "C:*", line 3032, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-39-68fb1c956dd7>", line 1, in <module>
    pd.read_sql("EXEC [dbo].[] '20140528'",conn)
  File "C:*", line 467, in read_sql
    chunksize=chunksize
  File "c:***", line 1404, in read_query
    columns = [col_desc[0] for col_desc in cursor.description]
TypeError: 'NoneType' object is not iterable
+5
source share
3 answers

Your sproc needs

SET NOCOUNT ON;

Without this, it sqlwill return rowcountfor a call that will return without a column name, causing an error NoneType.

+10
source

pd.read_sql() ; TypeError. :

import datetime as dt
import pyodbc
import pandas as pd

conn = pyodbc.connect('Trusted_Connection=yes', driver = '{SQL Server Native client 11.0}',server = '*****', database = '**')
cur = conn.cursor()
cur.execute("EXEC ******** '20140528'")

- , , .

+1
import datetime as dt
import pyodbc
import pandas as pd

conn = pyodbc.connect('Trusted_Connection=yes; driver =SQL Server Native client 
11.0; server = *****, database = **')
sqlSend = conn.cursor()
sqlSend.execute(f"EXEC ******** '20140528'")
conn.commint()
0
source

All Articles