Cursor.execute()
仅当它试图执行的SQL语句失败时才会引发异常,例如:
>>> cur.execute("This is not SQL")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: near "This": syntax error
或
>>> cur.execute("SELECT * FROM nonexistent_table;")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: no such table: nonexistent_table
一个有效的SQL语句没有正确地执行任何操作,它已经成功,没有失败,因此不会引发异常。你的
DELETE
语句在找不到
name
,所以没有错误。
您可以使用
Cursor.rowcount
属性。重新编写代码以使用属性如下:
name = input("Enter Student name you want to delete:")
cur.execute('DELETE FROM studentdata WHERE name = ?;', [name])
if cur.rowcount > 0:
print("Data Deleted Successfully")
conn.commit()
else:
print("No data found with this name:", name)
commit()
根据应用程序的不同,它在代码中的位置可能实际上应该移动到
if
/
else