62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
|
|
import mysql.connector
|
|
|
|
def get_connection_details():
|
|
details = {}
|
|
with open('setting.txt', 'r') as f:
|
|
for line in f:
|
|
key, value = line.strip().split(':', 1)
|
|
details[key.strip()] = value.strip()
|
|
return details
|
|
|
|
def alter_table_columns():
|
|
try:
|
|
config = get_connection_details()
|
|
conn = mysql.connector.connect(
|
|
host=config.get('Host'),
|
|
port=config.get('Port'),
|
|
user=config.get('Username'),
|
|
password=config.get('Password'),
|
|
database=config.get('Database')
|
|
)
|
|
cursor = conn.cursor()
|
|
|
|
# Check and rename 'unit' to 'plant'
|
|
cursor.execute("SHOW COLUMNS FROM extension_data LIKE 'unit'")
|
|
if cursor.fetchone():
|
|
print("Renaming 'unit' to 'plant'...")
|
|
cursor.execute("ALTER TABLE extension_data CHANGE COLUMN unit plant VARCHAR(255)")
|
|
print("Column 'unit' renamed to 'plant' successfully.")
|
|
else:
|
|
print("Column 'unit' does not exist or already renamed.")
|
|
|
|
# Check and drop 'source_file'
|
|
cursor.execute("SHOW COLUMNS FROM extension_data LIKE 'source_file'")
|
|
if cursor.fetchone():
|
|
print("Dropping 'source_file' column...")
|
|
cursor.execute("ALTER TABLE extension_data DROP COLUMN source_file")
|
|
print("Column 'source_file' dropped successfully.")
|
|
else:
|
|
print("Column 'source_file' does not exist or already dropped.")
|
|
|
|
# Check and drop 'sheet'
|
|
cursor.execute("SHOW COLUMNS FROM extension_data LIKE 'sheet'")
|
|
if cursor.fetchone():
|
|
print("Dropping 'sheet' column...")
|
|
cursor.execute("ALTER TABLE extension_data DROP COLUMN sheet")
|
|
print("Column 'sheet' dropped successfully.")
|
|
else:
|
|
print("Column 'sheet' does not exist or already dropped.")
|
|
|
|
conn.commit()
|
|
|
|
except Exception as e:
|
|
print(f"An error occurred: {e}")
|
|
finally:
|
|
if 'conn' in locals() and conn.is_connected():
|
|
cursor.close()
|
|
conn.close()
|
|
|
|
if __name__ == "__main__":
|
|
alter_table_columns()
|