Until pandas include the option of INSERT IGNORE in the default pd.DataFrame.to_sql class, here is the best solution:
from sqlalchemy.ext.compiler import compiles from sqlalchemy.sql.expression import Insert # adds the word IGNORE after INSERT in sqlalchemy @compiles(Insert) def _prefix_insert_with_ignore(insert, compiler, **kw): return compiler.visit_insert(insert.prefix_with('IGNORE'), **kw)
Now you can run your normal df.to_sql commands!
from sqlalchemy import create_engine from sqlalchemy.ext.compiler import compiles from sqlalchemy.sql.expression import Insert @compiles(Insert) def _prefix_insert_with_ignore(insert, compiler, **kw): return compiler.visit_insert(insert.prefix_with('IGNORE'), **kw) df.index.rename('index_column', inplace=True) sqlEngine = create_engine('mysql+pymysql://'+dbUser+':'+dbPass+'@'+dbHost+'/'+dbDbse, pool_recycle=3600) dbConnection = sqlEngine.connect() try: df.to_sql(dbTble, con=dbConnection, if_exists='append') except ValueError as vx: print(vx) except Exception as ex: print(ex) else: print("Table %s created successfully."%dbTble)
does not work get error
(sqlite3.OperationalError) near “IGNORE”: syntax error
[SQL: INSERT IGNORE INTO ib_trades (“Exch. Exec. ID”, “Security Type”, “Last Trading Day”, “Strike”, “Put/Call”, “……….))]
(Background on this error at: https://sqlalche.me/e/14