Python MySQL – Truncating a table

MySQL is an open-source relational database management system. Here we will be creating a Python script that will truncate all data within a specified table. By truncating a table we will be removing all rows from a table, but the table structure and its columns, constraints, indexes will remain. You may also be interested in updating data, deleting data and retrieving data.

Full source code for this project can be found below:

import mysql.connector

mydb = mysql.connector.connect(
host="",
user="",
password="",
database="")
mycursor = mydb.cursor()
mycursor.execute("TRUNCATE TABLE Table_1")
mycursor.close()
mydb.close()
   

Leave a Reply

Discover more from Scriptopia

Subscribe now to keep reading and get access to the full archive.

Continue reading