Skip to content

Writing data to mysql database using df.to_sql method gives exception #61711

@MohammadHilal1

Description

@MohammadHilal1

In my airflow project i am trying to load data to mysql database using df.to_mysql method but it gives me exception
AttributeError: 'Connection' object has no attribute 'cursor'

The code i am trying to execute is
def load_market_data(flattened_df_json):
records = json.loads(flattened_df_json)
df = pd.DataFrame(records)
df['from'] = pd.to_datetime(df['from']).dt.date
df['volume'] = df['volume'].astype('Int64')
engine = create_engine("mysql+pymysql://root:1111@localhost:3307/etl_project")
with engine.connect() as conn:
print("MySQL connection test:", conn.execute("SELECT 1").scalar())
try:
with engine.begin() as connection:
df.to_sql(name="market_data", con=connection, if_exists="append", index=False)
print("✅ Data loaded successfully")
except Exception as e:
print("Exception while inserting to db:", str(e))
raise

pandas version is 2.3.0
sqlalchemy version is 1.4.54

Metadata

Metadata

Assignees

No one assigned

    Labels

    IO SQLto_sql, read_sql, read_sql_query

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions