9951 explained code solutions for 126 technologies


python-mysqlHow do I truncate a MySQL table using Python?


To truncate a MySQL table using Python, you can use the TRUNCATE() method of the MySQLdb library. This method is used to delete all rows from a table without deleting the table itself.

Example code

import MySQLdb

db = MySQLdb.connect("localhost","user","password","database")

cursor = db.cursor()

cursor.execute("TRUNCATE TABLE table_name")

db.close()

The code above will delete all rows from the table table_name in the database.

Code explanation

  1. import MySQLdb: This imports the MySQLdb library, which is used to connect to and work with a MySQL database.

  2. db = MySQLdb.connect("localhost","user","password","database"): This connects to the MySQL database with the given credentials (user and password) and selects the database database.

  3. cursor = db.cursor(): This creates a cursor object which is used to execute queries.

  4. cursor.execute("TRUNCATE TABLE table_name"): This executes the TRUNCATE() method which deletes all rows from the table table_name.

  5. db.close(): This closes the connection to the database.

Helpful links

Edit this code on GitHub