简体   繁体   中英

cannot get sqlalchemy and pandas (to_sql) to write an dataframe index date into a MySQL DB

I read from an API the following data into a pandas dataframe:

在此处输入图像描述

Now, I want to write this data into a MySQL-DB-table, using pandas to_sql:

在此处输入图像描述

In MySQL, the column is set up correctly, but has not written the values:

在此处输入图像描述 在此处输入图像描述

Then I looked in the debugger to show me the dataframe:

在此处输入图像描述

I thought it would maybe a formatting issue, and added the following lines:

在此处输入图像描述

In the debugger, it looks now fine:

在此处输入图像描述

But now, in the database, it wants to write the index column as text

在此处输入图像描述

... and interrupts the execution with an error:

在此处输入图像描述

Is there a way to get this going, aka to write df index data as date into a MySQL DB using pandas to_SQL in connection with a sqlalchemy engine?

Edit: Table schema:

在此处输入图像描述

DataFrame Header:

在此处输入图像描述

It seems you are using Date column as primary key. I would suggest not to use that as primary key instead you should use Date + Ticker as primary key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM