Python MariaDB – Delete Query using PyMySQL
DELETE is an SQL query, that is used to delete one or more entries from a table with a given condition. To connect with MariaDB database server with Python, we need to import pymysql client. After connecting with the database in MySQL we can create tables in it and can manipulate them.
Syntax:
DELETE FROM TABLE_NAME WHERE ATTRIBUTE_NAME = ATTRIBUTE_VALUE
Example 1: Below is a program to delete a query from the table in the database.
Python3
import pymysql
Host = "localhost"
User = "user"
Password = ""
database = "GFG"
conn = pymysql.connect(host = Host, user = User,
password = Password, database)
cur = conn.cursor()
query = f "DELETE FROM PRODUCT WHERE PRODUCT_TYPE = 'PRI'"
cur.execute(query)
conn.commit()
conn.close()
|
Output :
BeforeAfter
In the above program, from table PRODUCT a single query is deleted from the table having the PRODUCT_TYPE attribute value PRI.
Example 2: Let us look at another example for queries in a table.
Python3
import pymysql
conn = pymysql.connect( 'localhost' , 'user' ,
'password' , 'database' )
cur = conn.cursor()
query = f "DELETE FROM PRODUCT WHERE price < 2000"
cur.execute(query)
conn.commit()
conn.close()
|
Output :
BeforeAfter
In the above program, from table PRODUCT a single query is deleted from the table where a price less than 2000.
Last Updated :
14 Oct, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...