将dataframe转换为sql-将数据附加到现有表而不重复

omqzjyyz  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(390)

我有一个mysql表feinstaub,其中的列(创建于,pm2.5,pm10,条目号)c4是唯一的。我有一个列名称相等的Dataframe。与sql表相比,这个Dataframe中有新值和已有值。我使用此行将Dataframe发送到SQLServer。

df.to_sql("Feinstaub", con=engine, if_exists="append", index=False)

它只有在Dataframe中没有重复符时才起作用。如果有什么事。重视它所起的作用。我找到了这个解决方案:pandas to \u sql()来更新db中的唯一值?

with engine.begin() as cn:
   sql = """INSERT INTO myFinalTable (Col1, Col2, Col3, ...)
            SELECT t.Col1, t.Col2, t.Col3, ...
            FROM myTempTable t
            WHERE NOT EXISTS 
                (SELECT 1 FROM myFinalTable f
                 WHERE t.MatchColumn1 = f.MatchColumn1
                 AND t.MatchColumn2 = f.MatchColumn2)"""

   cn.execute(sql)

我最终得到了这个:

df.to_sql("temp_feinstaub_wohnzimmer", con=engine, if_exists="replace", index=False)
with engine.begin() as cn:
   sql = """INSERT INTO feinstaub (created_at, 'PM 2.5' , 'PM 10', entry_id)
            SELECT t.Column1, t.Column2, t.Column3 ,t.Column4
            FROM temp_feinstaub_wohnzimmer t
            WHERE NOT EXISTS
                (SELECT 1 FROM feinstaub f
                 WHERE t.MatchColumn1 = f.MatchColumn1
                 AND t.MatchColumn2 = f.MatchColumn2
                 AND t.MatchColumn3 = f.MatchColumn3
                 AND t.MatchColumn4 = f.MatchColumn4)"""

   cn.execute(sql)

它引发了一个sql语法错误。我试图重命名f.matchcolumn,但仍然给我一个sql语法错误?
编辑:我现在使用这段代码,它与backticks工作谢谢!但这又引起了另一个错误;)


# Send the Data to SQL database

df.to_sql("temp_feinstaub_wohnzimmer", con=engine, if_exists="replace", index=False)
with engine.begin() as cn:
   sql = """INSERT INTO feinstaub (created_at, `PM 2.5` , `PM 10`, entry_id)
            SELECT t.created_at, t.`PM 2.5`, t.`PM 10` ,t.entry_id
            FROM temp_feinstaub_wohnzimmer t
            WHERE NOT EXISTS
                (SELECT 1 FROM feinstaub f
                 WHERE t.created_at = f.created_at
                 AND t.`PM 2.5` = f.`PM 2.5`
                 AND t.`PM 10` = f.`PM 10`
                 AND t.entry_id = f.entry_id)"""

   cn.execute(sql)

现在我得到以下错误:

sqlalchemy.exc.IntegrityError: (_mysql_exceptions.IntegrityError) (1062, "Duplicate entry '3825' for key 'entry_id'") [SQL: 'INSERT INTO feinstaub_wohnzimmer (created_at, `PM 2.5` , `PM 10`, entry_id)\n            SELECT t.created_at, t.`PM 2.5`, t.`PM 10` ,t.entry_id\n            FROM temp_feinstaub_wohnzimmer t\n            WHERE NOT EXISTS\n                (SELECT 1 FROM feinstaub_wohnzimmer f\n                 WHERE t.created_at = f.created_at\n                 AND t.`PM 2.5` = f.`PM 2.5`\n                 AND t.`PM 10` = f.`PM 10`\n                 AND t.entry_id = f.entry_id)']
iklwldmw

iklwldmw1#

这对我来说很有用。。。我可以多次执行脚本,只有新的值才能进入mysql数据库。

from sqlalchemy import exc
num_rows = len(df)

# Iterate one row at a time

for i in range(num_rows):
    try:
        #Try inserting the row
        df.iloc[i:i+1].to_sql(name="feinstaub_wohnzimmer",con = engine,if_exists = 'append',index=False)
    except exc.IntegrityError:
        #Ignore duplicates
        pass

相关问题