Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flask-sqlalchemy check if data already exits in table before Inserting

I am trying to insert some values into table using flask sqllite methods but I am not sure how to make the statement equivalent to Insert into table if not exits , the below method adds the data but if already present I want to ignore it kindly guide on the same

 pl1 = mydb(employee_name='JOhn doe')
  db.session.add(pl1)
  db.commit()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Maybe this

pl1 = mydb(employee_name='JOhn doe')

if not db_session.query(mydb).filter_by(employee_name='JOhn doe').first():
    db_session.add(pl1)
    db_session.commit()

I usually use db or db_session. I don’t know if your db.session is a typo. In any case it should be easy to adjust the syntax.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading