2

Was trying to query a TopSpeed DB and came up with this error:

AttributeError: 'pyodbc.Cursor' object has no attribute 'cursor'

Basically, the application will query a legacy TopSpeed DB then save it by chunks of 1000 rows to a CSV file.

Here is my code snippet:

import pandas.io.sql as psql

FILE_EXT = 'csv'
BASE_DIR = Path(__file__).resolve().parent

TPS_CONNECTION_STRING = (
    'Driver={Topspeed ODBC Driver (Read-Only)};'
    'DBQ=X:\\;'
    'Extension=tps;'
)

query = 'SELECT * FROM vCUSTTRANc;'

filename = 'tpstest'

conn = pyodbc.connect(TPS_CONNECTION_STRING, autocommit=True)
cursor = conn.cursor()

df = psql.read_sql(sql=query, con=cursor, chunksize=1000)
cursor.close()

filename = f'{filename}.{FILE_EXT}'
file = os.path.join(BASE_DIR, filename)

for idx, chunk in enumerate(df):
   chunk.to_csv(
      file,  # noqa
      index=False,
      header=(idx == 0),
      mode='a'
   )
joween
  • 61
  • 2
  • 8
  • Although it is unclear what `psql` is, but you are passing cursor as connection in `psql.read_sql(sql=query, con=cursor, chunksize=1000)` – Maurice Meyer Sep 15 '21 at 07:10
  • sorry, psql is from `import pandas.io.sql as psql` I will update the question. I just followed an answer here: [link](https://stackoverflow.com/a/14487936/15448303) – joween Sep 15 '21 at 07:14
  • You want: `psql.read_sql(sql=query, con= conn, chunksize=1000)` – Maurice Meyer Sep 15 '21 at 07:22
  • yeah. just figured it out seconds ago. I'm dumb. lol. Thanks btw. – joween Sep 15 '21 at 07:23

0 Answers0