Here, we have a basic program example to delete the data from a table using Mysql in Python and Java.
Code to delete data in Python language
import mysql.connector
con = mysql.connector.connect(host="localhost", user="root", passwd="root", database="coder")
cur = con.cursor()
#accepting record to be remove
bookname=input("Enter the Book Name")
#delete query, to remove records
cur.execute("delete from Book where bookname='{}'".format(bookname))
print("Record Removed")
con.commit()
con.close()
Code to delete data in Java language
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DeleteData{
static final String DB_URL = "jdbc:mysql://localhost/TestDb";
static final String USER = "root";
static final String PASS = "vsit@123";
static final String QUERY = "SELECT Id, Name, CountryName, Age FROM DemoTable";
public static void main(String[] args) {
try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
) {
String sql = "DELETE FROM DemoTable " +
"WHERE id = 10";
stmt.executeUpdate(sql);
ResultSet rs = stmt.executeQuery(QUERY);
System.out.println("Record is deleted successfully......");
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}