darksky
darksky

Reputation: 21019

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

Upvotes: 3

Views: 222

Answers (1)

Sean Vieira
Sean Vieira

Reputation: 159865

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).

Upvotes: 2

Related Questions