-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmy_sql_table_drop.py
More file actions
64 lines (42 loc) · 1.62 KB
/
Copy pathmy_sql_table_drop.py
File metadata and controls
64 lines (42 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 25 23:58:25 2018
@author: SebastianArr
"""
import mysql.connector as mdb
import getpass
# Connect to the MySQL instance
db_host = 'localhost'
db_user = 'root'
db_name = 'securities_master'
db_pass = getpass.getpass('Enter database password: ')
con = mdb.connect(host=db_host, user=db_user, passwd=db_pass, db=db_name)
# create dictionary with needed tables
TABLES = ['equities_data_vendors','equities_exchanges','equities_symbols','equities_combinations','equities_daily_prices',
'cryptocurrencies_data_vendors','cryptocurrencies_exchanges','cryptocurrencies_symbols',
'cryptocurrencies_symbols_reference_currencies','cryptocurrencies_combinations','cryptocurrencies_daily_prices']
# check if this is really wanted! --> don't delete tables by accident
print("You are going to drop the following tables:")
print (TABLES)
confirmation = input('If you are sure about this type \'yes\': ')
# drop required tables
if confirmation=='yes':
for name in TABLES:
try:
print("Drop table: ", name)
cur = con.cursor()
cur.execute("SET FOREIGN_KEY_CHECKS = 0;")
cur.execute("Drop TABLE `%s`" % (name))
cur.execute("SET FOREIGN_KEY_CHECKS = 1;")
cur.close()
except mdb.Error as err:
if err.errno == mdb.errorcode.ER_BAD_TABLE_ERROR:
print("Warning: Table does not exist. It cannot be dropped!")
else:
print(err.msg)
else:
print("OK")
else:
print('Dropping of tables aborted.')
con.close()