熊猫数据框to_sql忽略外键约束

时间:2020-04-29 11:36:55

标签: pandas sqlalchemy

我目前正在做一个MySQL结构如下的项目: enter image description here

我需要将python pandas数据框的apply数据加载到MySQL apply表中。但是由于原始的熊猫数据帧具有ACS Tag中没有的Episode_id元素(违反了外键约束)。在这种情况下,我想跳过违反MySQL外约束键的这些行,并继续加载其他行。以下是经过大量搜索后尝试的代码,包括sqlachemy文档:

import mysql.connector as connector
from sqlalchemy import create_engine

engine = create_engine(
    'mysql+mysqlconnector://root:12345' +
    '@localhost:3306/icd_10_study', 
     echo=False)

with engine.begin() as cnn:
    insert_sql = """INSERT IGNORE INTO apply (episode_id, standard_id) VALUES (%(episode_id)s, 
           % (standard_id)s)"""
    cnn.execute(insert_sql % insert_sql)
applies.to_sql(name='apply', con=engine, 
        if_exists = 'append', index=False)

但是此代码报告编程错误。寻找在这种情况下可以提供帮助的人。我尝试阅读以下页面:https://docs.sqlalchemy.org/en/13/core/connections.html。但是在此页面上,我不了解“ table1”的含义,因此根本无法解决我的问题。在这里寻求帮助。

0 个答案:

没有答案