Dropping tables with MySQLdb
Dropping a table through MySQLdb
is very straightforward.
cursor.execute("DROP TABLE %s" %('barge'))
Of course, for the sake of feedback, it is worth sandwiching this in a try...except
structure. As with the DROP TABLE
statement in MySQL, you can usually just pass this statement with an IF EXISTS
conditional and be done with it:
execution = cursor.execute("DROP TABLE IF EXISTS %s" %('barge'))
Note that, as before, the conditional element causes MySQL to always return a 0 value for successful execution. So a test for the value of execution will always test true for 0. If you want an exception if the table does not exist, use the other DROP
statement in a try...except
statement.
Get MySQL for Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.