my_query="DELETE FROM student WHERE id=5 "
curs=my_conn.execute(my_query)
print("Record Deleted ")
my_conn.commit()
Output
Record Deleted
q="DELETE from student WHERE class='Four' "
try:
r_set=my_conn.execute(q)
print("Records deleted")
my_conn.commit()
except sqlite3.Error as my_error:
print("error: ",my_error)
Output
Records deleted
We can change the the query to q="DELETE from student1 WHERE class='Four' and get the error message
error: no such table: student1
q="DELETE from student WHERE class='Four' "
try:
r_set=my_conn.execute(q)
print("No of Records deleted : ",r_set.rowcount)
my_conn.commit()
except sqlite3.Error as my_error:
print("error: ",my_error)
Output
No of Records deleted : 9
We can also use changes() to get number of records deleted. ( this code is used after executing above code )
x=my_conn.execute('''select changes()''')
id=x.fetchone()
print(id[0])
9
my_data=[("Three")]
q="DELETE FROM student WHERE class=?"
try:
r_set=my_conn.execute(q,my_data)
print("Records deleted : ",r_set.rowcount)
my_conn.commit()
except sqlite3.Error as my_error:
print("error: ",my_error)
Output
Records deleted : 3
Deleting records using more than 1 column
my_data=[("Seven"),("Six")]
q="DELETE FROM student WHERE class=? or class=?"
try:
r_set=my_conn.execute(q,my_data)
print("Number of Records deleted : ",r_set.rowcount)
my_conn.commit()
except sqlite3.Error as my_error:
print("error: ",my_error)
Output
Number of Records deleted : 17
q="DELETE FROM student"
try:
r_set=my_conn.execute(q)
print("No of Records deleted : ",r_set.rowcount)
my_conn.commit()
except sqlite3.Error as my_error:
print("error: ",my_error)
Output
No of Records deleted : 35
All records are only deleted. The structure of the table is not removed.
#query="DELETE FROM student"
query='DROP TABLE student'
try:
r_set=my_conn.execute(query)
print("No of Records deleted : ",r_set.rowcount)
my_conn.commit()
except sqlite3.Error as my_error:
print("error: ",my_error)
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.