我使用以下get_db()
函数来获取我的SQLite数据库:
def get_db():
top = _app_ctx_stack.top
if not hasattr(top, 'sqlite_db'):
top.sqlite_db = sqlite3.connect(app.config['DATABASE'])
return top.sqlite_db
我相信我不会在这里使用Python SQLite游标。如果我执行以下操作:
db = get_db()
db.execute('DELETE FROM table_name WHERE id=3')
db.commit()
如何检查DELETE中传递的ID是否存在?我尝试在sqlite3.Cursor中使用rowcount,但这不适用于我在这里检索数据库的方式。
我该如何检查?如果传递的ID不存在,我需要它返回404。
由于
答案 0 :(得分:1)
为什么不首先查询ID,如果不存在则抛出404。类似的东西:
cur = g.db.query('select * from table_name where id=3')
if cur.rowcount <= 0:
* abort(404)
''Run delete command here''