如何在python中将dataframe(包含excel表)推送到mysql中?

2024-04-26 12:55:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python从S3读取数据,并试图写入mysql数据库,但是在这样做的时候我遇到了一个错误。你知道吗

非常感谢您的帮助!你知道吗

from pandas.io import sql
import pymysql
from sqlalchemy import create_engine

engine = create_engine('mysql+pymysql://' + user + ':' + passw + '@' + host + ':' + str(port) + '/' + database , echo=False)

df.to_sql(con=engine, name='pna_data', if_exists='replace')

错误:

目前,该公司的内部资料,\n\t ^{IMEI 2浮点(53),\n\tProduct Model文本,\n\tPurchase Date日期时间,\n\tService Type文本,\\n\t\n\t\n\t IsWarrantyApplicable文字,\n\t Is CID文字,\n\n\t Create Time日期时间,\n\t Apply for Parts Time日期时间,\n\t{}文字,\n\n\t Is CID文字,\n\n\t Create Time日期时间,日期时间,\n\n\t Create Time日期日期时间,日期时间\ \ \ \ \n\n\t Apply for Parts Time日期日期日期时间,日期时间\ \ \ \n\n\n\n\t ^{{Quantity at OnePlus - Bangalore Main Warehouse浮点数(53),\n\tQuantity at Regenersis India PVT LTD. Central warehouse - Bangalore浮点数(53),\n\tIntransit Quantity浮点数(53),\n\tShipment Date日期时间、\n\tShipping Status文本、\n\tLogistic name文本、\n\tAWB No.文本、\n\tTAT浮点(53)、\n\tRequest Status文本、\n\tPart status文本、\n\tPNA文本、\n\tCreatedDate文本、\n\tUpdatedDate文本、\n\tPNA_Resolved_Date文本\n\n']

(此错误的背景信息位于:http://sqlalche.me/e/2j85


Tags: from文本importsqldatetime错误create
1条回答
网友
1楼 · 发布于 2024-04-26 12:55:49

看起来它实际上是获取了一些数据行并试图从中创建一个表。你知道吗

CREATE TABLE pna_data ( ...
    Quantity at Regenersis India PVT LTD. Central warehouse - Bangalore FLOAT(53)
    ...
)

因此,你得到了错误

Identifier name ... is too long

我建议您将输入数据中的列重命名为不太详细的列,并对数据进行反规范化,例如

quantity   | location                                       | country
     -|                        |    -
100        | "Regenersis India PVT LTD. Central warehouse"  | "Bangalore"

如果愿意,可以在尝试执行df.sql之前重命名/修改/分离Pandas中的列

相关问题 更多 >