• Home
  • Releases
  • Submit Vuln
  • Press
  • About
  • PGP
  • Contact
    • Contact
    • Submit Vuln
    • VDP
  • Tutorials
    • All Posts
    • Photoshop on Linux
    • macOS on Linux
  • Supporters
  • Projects
  • Training
Sick Codes - Security Research, Hardware & Software Hacking, Consulting, Linux, IoT, Cloud, Embedded, Arch, Tweaks & Tips!
  • Home
  • Releases
  • Submit Vuln
  • Press
  • About
  • PGP
  • Contact
    • Contact
    • Submit Vuln
    • VDP
  • Tutorials
    • All Posts
    • Photoshop on Linux
    • macOS on Linux
  • Supporters
  • Projects
  • Training
No Result
View All Result
Sick Codes - Security Research, Hardware & Software Hacking, Consulting, Linux, IoT, Cloud, Embedded, Arch, Tweaks & Tips!
  • Home
  • Releases
  • Submit Vuln
  • Press
  • About
  • PGP
  • Contact
    • Contact
    • Submit Vuln
    • VDP
  • Tutorials
    • All Posts
    • Photoshop on Linux
    • macOS on Linux
  • Supporters
  • Projects
  • Training
No Result
View All Result
Sick Codes - Security Research, Hardware & Software Hacking, Consulting, Linux, IoT, Cloud, Embedded, Arch, Tweaks & Tips!
No Result
View All Result
Home Tutorials

Python: Pandas.to_sql INSERT IGNORE the correct way (sqlalchemy)

by Sick Codes
June 27, 2020
in Tutorials
1

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)




Next Post
Raspberry Pi 4 Touch Screen Drivers

Raspberry Pi 4 Touch Display Drivers ALL Size 2.4, 2.8, 3.2, 3.5, 5, 7 inch displays & drivers.

How To Install Latest Python In Ubutnu 18.04 (Shell Commands & Docker/Dockerfile)

CVE-2020-15590 - Private Internet Access VPN for Linux – Exposure of Sensitive Information to an Unauthorized Actor

Comments 1

  1. net says:
    2 years ago

    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

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result
  • Home
  • Releases
  • Submit Vuln
  • Press
  • About
  • PGP
  • Contact
    • Contact
    • Submit Vuln
    • VDP
  • Tutorials
    • All Posts
    • Photoshop on Linux
    • macOS on Linux
  • Supporters
  • Projects
  • Training

© 2017-2021 Sick.Codes

@sickcodes

@sickcodes

@sickcodes

Discord Server

sickcodes.slack.com

t.me/sickcodeschat

./contact_form