Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask Catch SQLite Query Issues

I'm writing an API in Flask, and my HTTP DELETE method deletes a record in SQLite according to its ID passed as a JSON.

Assuming the ID passed does not exist, no error is thrown and success 200 is returned. Is that the expected behaviour? Should I throw an HTTP error? If so, how can I check if db.execute() query did not finish properly?

Sorry but this is my first time writing an API in Flask and I'm still learning it. Thanks

like image 603
darksky Avatar asked Aug 23 '26 06:08

darksky


1 Answers

The sqlite3.Cursor object has a rowcount attribute that will be greater than or equal to 1 if the ID existed.

Alternately, since DELETE is idempotent you could assume that every ID you are provided was valid at some point and always return a 200 or 204 (although a 404 is better if the resource never could have existed).

like image 184
Sean Vieira Avatar answered Aug 25 '26 21:08

Sean Vieira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!