1

How can I delete a record using its id?

I currently have the following code:

@app.route('/delete/<cid>')
def dele(cid):
    # o= catagory.query.get(cid)
    a=db.session.query(catagory).filter(bool(cid)).delete(synchronize_session='fetch')
    db.session.commit()
    return "deleted"

But it will delete the entire data in that table. I only want to delete a specific record by specifying its id

I also use this code to implement that :

@app.route('/delete/<cid>')
def dele(cid):
    k=catagory.query.get()
    e = catagory.query.filter_by(cid='"+cid+"').first()
    print (cid)
    a = db.session.delete(e)
    db.session.commit()
    return "deleted"

My table name is catagory and cid is the column in catagory table

haggai_e
  • 4,689
  • 1
  • 24
  • 37
aiswarya r
  • 11
  • 1
  • 1
    Note that `filter(bool(cid))` is equivalent to either `filter(False)` or `filter(True)`, depending on what `cid` is. `'"+cid+"'` is just a string containing a quote, a plus, the text cid, and again a plus and a quote. – Ilja Everilä Jun 13 '18 at 08:20

0 Answers0