By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,012 Members | 1,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,012 IT Pros & Developers. It's quick & easy.

I am trying to get an SQL Table into a Pandas DataFrame and get a cursor error

P: 1
I am just trying to get an SQL Server table into a Pandas DataFrame and copied this code from the internet, obviously added my local details and credentials ie XXXX etc. Which I think works OK. When I try to run the next bit I get the messages below.

As a newbee please can someone help me as I have been going round in circles for 3 hours now!!!!! Many thanks

Expand|Select|Wrap|Line Numbers
  1. import pyodbc
  2. import pandas as pd
  4. conn = pyodbc.connect
  5. (r'DRIVER={ODBC Driver 13 for SQL Server};' +
  6.  ('SERVER={server},{port};'   +
  7.   'DATABASE={database};'      +
  8.   'UID={username};'           +
  9.   'PWD={password}').format(
  10.                 server= 'XXXXXX',
  11.                   port= 'XXXXXX',
  12.               database= 'XXXXXXX',
  13.               username= 'XXXXXXX',
  14.               password= 'XXXXXXXXX'))
  16. SQL = "select [PASID], [TeamReferralStartDate], [TeamReferralEndDate], ]TeamCodeDescription] FROM live.tbl_ref_Mart"
  18. SQL_Query = pd.read_sql_query (SQL ,conn)
Above code gives me

Expand|Select|Wrap|Line Numbers
  1.   File "<ipython-input-68-57057e3d73eb>", line 3, in <module>
  2.     SQL_Query = pd.read_sql_query (SQL ,conn)
  4.   File "C:\Users\SMITHAL\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\", line 332, in read_sql_query
  5.     chunksize=chunksize,
  7.   File "C:\Users\SMITHAL\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\", line 1645, in read_query
  8.     cursor = self.execute(*args)
  10.   File "C:\Users\SMITHAL\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\", line 1590, in execute
  11.     cur = self.con.cursor()
  13. AttributeError: 'builtin_function_or_method' object has no attribute 'cursor'
Dec 20 '19 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.