Skip to content
Related Articles

Related Articles

Improve Article

Python MariaDB – Drop Table using PyMySQL

  • Last Updated : 14 Oct, 2020

MariaDB is an open source Database Management System and its predecessor to MySQL. The pymysql client can be used to interact with MariaDB similar to that of MySQL using Python.

In this article we will look into the process of Dropping a table from a database using pymysql. To drop a table use any of the below syntax:

Syntax :

DROP TABLE tablename;

DROP TABLE IF EXISTS tablename;

The following programs will help you understand this better.

Example 1 :



 Program to demonstrate drop if exists. We will try to drop a table which does not exist in the above database.

Python3




import pymysql
  
# Create a connection object
# IP address of the MySQL database server
Host = "localhost"  
  
# User name of the database server
User = "user"      
  
# Password for the database user
Password = ""           
  
database = "GFG"
  
conn  = pymysql.connect(host=Host, user=User, password=Password, database)
  
# Create a cursor object
cur  = conn.cursor()
  
  
query = f"DROP TABLE IF exists Employee"
  
cur.execute(query)
  
conn.close()

Output :

  • Before

tables

  • After

employee table dropped

Example 2 : 

Here we will drop the student table.

Python3




import pymysql
  
# Create a connection object
  
conn  = pymysql.connect('localhost', 'user', 'password', 'database')
  
# Create a cursor object
cur  = conn.cursor()
  
  
query = f"DROP TABLE IF exists Student"
  
cur.execute(query)
  
rows = cur.fetchall()
conn.close()
  
for row in rows :
    print(row)

Output :

  • Before

table

  • After

student table dropped

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :