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'
)