[英]Check if registry has been deleted with python
How can I check if database data is missing?如何检查数据库数据是否丢失?
if cur.execute('DELETE FROM products WHERE shop = 5'):
print('deleted sucess')
This code doesn't work and I didn't find how I can validate if the data has been deleted此代码不起作用,我没有找到如何验证数据是否已被删除
You can delete then attempt to select what was deleted.您可以删除然后尝试选择已删除的内容。 This code should tell you if the delete was successful, though it's not very efficient... the database should delete the data and if it doesn't then you should pick a new database.
这段代码应该告诉你删除是否成功,尽管它不是很有效……数据库应该删除数据,如果没有,那么你应该选择一个新数据库。
cur.execute('DELETE FROM products WHERE shop = 5')
cur.execute('SELECT * from products where shop = 5')
if not cur.fetchone():
print('delete successful')
I am not 100% sure the motivation behind this extra check, but you can verify the cur.rowcount
value, which returns the number of "affected rows" in your last query:我不是 100% 确定此额外检查背后的动机,但您可以验证
cur.rowcount
值,该值返回上次查询中“受影响的行”的数量:
This read-only attribute specifies the number of rows that the last
.execute*()
produced (for DQL statements likeSELECT
) or affected (forDML
statements likeUPDATE
orINSERT
).此只读属性指定最后一个
.execute*()
生成的行数(对于SELECT
等 DQL 语句)或受影响的行数(对于UPDATE
或INSERT
等DML
语句)。 [9][9]
cur.execute('DELETE FROM products WHERE shop = 5')
affected_rows = cur.rowcount
print("Rows affected: {affected_rows}".format(affected_rows=affected_rows))
if affected_rows > 0:
print('deleted success')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.