Files
0908_--USER--/create_pizza_table.py
2025-09-08 16:21:07 +08:00

58 lines
1.7 KiB
Python

import mysql.connector
from mysql.connector import Error
# --- Database Configuration ---
DB_HOST = "mysql.theaken.com"
DB_PORT = 33306
DB_NAME = "db_A027"
DB_USER = "A027"
DB_PASSWORD = "E1CelfxqlKoj"
TABLE_NAME = "pizzas" # Changed table name to be more descriptive
def create_table():
"""Connects to the database and creates the specified table."""
conn = None
try:
# Establish database connection
conn = mysql.connector.connect(
host=DB_HOST,
port=DB_PORT,
database=DB_NAME,
user=DB_USER,
password=DB_PASSWORD
)
if conn.is_connected():
cursor = conn.cursor()
# Drop the table if it already exists
cursor.execute(f"DROP TABLE IF EXISTS {TABLE_NAME}")
# Create the new table
# The user's request had conflicting column names and descriptions.
# Based on the filename "pizza建立table.txt", I am creating a "pizzas" table.
# The columns are id, name, price, and size.
create_table_query = f"""
CREATE TABLE {TABLE_NAME} (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
price INT NOT NULL,
size VARCHAR(50)
)"""
cursor.execute(create_table_query)
print("資料表建立成功!")
except Error as e:
print(f"資料庫操作失敗: {e}")
finally:
# Close the connection
if conn and conn.is_connected():
cursor.close()
conn.close()
if __name__ == '__main__':
create_table()