Last active
April 28, 2020 20:25
-
-
Save jamescalam/b316c1714c30986fff58c22b00395cc0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def drop(self, tables): | |
"""Pass a table or list of table names to drop. | |
Keyword arguments: | |
tables -- a single table name as a string, or a list of table names as | |
strings. For [dbo].[data] we would input "data" | |
""" | |
# check if single or list | |
if isinstance(tables, str): | |
# if single string, convert to single item in list for for-loop | |
tables = [tables] | |
cursor = self.cnxn.cursor() # create execution cursor | |
for table in tables: | |
# check for pre-existing table and delete if present | |
try: | |
query = ("IF OBJECT_ID ('["+table+"]', 'U') IS NOT NULL " | |
"DROP TABLE ["+table+"]") | |
cursor.execute(query) # execute | |
except pyodbc.ProgrammingError as error: | |
print("Warning:\n{}".format(error)) # print error as a warning | |
self.cnxn.commit() # commit drop(s) to SQL Server | |
print("Tables dropped.") # update user | |
# append query to our SQL code logger | |
self.query += ("\n\n-- drop table\n" + query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment