my_cursor = my_connect.cursor() #
my_cursor.execute("DELETE FROM student WHERE id=25")
my_connect.commit()
print("Rows Deleted = ",my_cursor.rowcount)
my_connect.close()
Output
Rows Deleted = 1
We used rowcount to get the number of records deleted in our student table. my_cursor = my_connect.cursor() #
try:
my_cursor.execute("DELETE FROM student WHERE Class='Five'")
my_connect.commit()
print("Rows Deleted = ",my_cursor.rowcount)
except mysql.connector.Error as my_error:
print(my_error)
my_connect.close()
Output is here
Rows Deleted = 3
my_cursor = my_connect.cursor() #
try:
query="DELETE FROM student WHERE Class=%s"
my_data=['Six']
my_cursor.execute(query,my_data)
my_connect.commit()
print("Rows Deleted = ",my_cursor.rowcount)
except mysql.connector.Error as my_error:
print(my_error)
my_connect.close()
Output is here
Rows Deleted = 7
my_cursor = my_connect.cursor() #
try:
query="DELETE FROM student WHERE id=%s"
my_data=[7]
my_cursor.execute(query,my_data)
my_connect.commit()
print("Rows Deleted = ",my_cursor.rowcount)
except mysql.connector.Error as my_error:
print(my_error)
my_connect.close()
Rows Deleted = 1
executemany()
. Here is the code.
my_cursor = my_connect.cursor() #
try:
query="DELETE FROM student WHERE id=%s"
my_data=[(7,),(5,),(4,),(3,)]
my_cursor.executemany(query,my_data)
my_connect.commit()
print("Rows Deleted = ",my_cursor.rowcount)
except mysql.connector.Error as my_error:
print(my_error)
my_connect.close()
Output is here
Rows Deleted = 4
Deleting all records
my_cursor = my_connect.cursor() #
try:
query="DELETE FROM student"
my_cursor.execute(query)
my_connect.commit()
print("Rows Deleted = ",my_cursor.rowcount)
except mysql.connector.Error as my_error:
print(my_error)
my_connect.close()
Rows Deleted = 24
We can also use TRUNCATE command to delete all records of a table.
my_cursor = my_connect.cursor() #
try:
query="TRUNCATE student"
my_cursor.execute(query)
my_connect.commit()
print("Rows Deleted = ",my_cursor.rowcount)
except mysql.connector.Error as my_error:
print(my_error)
my_connect.close()
Rows Deleted = 0
from sqlalchemy import create_engine
engine = create_engine("mysql+mysqldb://userid:password@localhost/my_tutorial")
query ='select id from student where id= 6'
my_data=engine.execute(query)
print("Number or matching records :", my_data.rowcount)
if my_data.rowcount==1 :
my_data=engine.execute("DELETE FROM student WHERE id=6")
print("Rows Deleted = ",my_data.rowcount)
else:
print("No matching record")
Using as connection object
import mysql.connector
my_connect = mysql.connector.connect(
host="localhost",
user="userid",
passwd="password",
database="my_tutorial"
)
####### end of connection ####
my_cursor = my_connect.cursor(buffered=True)
query ="select id from student where id=5 "
my_cursor.execute(query)
if(my_cursor.rowcount ==1):
my_cursor.execute("DELETE FROM student WHERE id=5")
my_connect.commit()
print("Rows Deleted = ",my_cursor.rowcount)
my_connect.close()
else:
print("No matching record")
Delete MySQL table query
More on Error handling15-07-2020 | |
i want few modification while deleting record from MySQL table. if record doesn't exist then message will appear and if exists then delete that record and message will occur for successfully deletion. |
18-07-2020 | |
Check if record exists then delete. .. This part is added at the end now. Thanks |