47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
|
|
import mysql.connector
|
|
from mysql.connector import errorcode
|
|
|
|
# Assuming the same DB credentials as the previous task
|
|
config = {
|
|
'user': 'A027',
|
|
'password': 'E1CelfxqlKoj',
|
|
'host': 'mysql.theaken.com',
|
|
'port': 33306,
|
|
'database': 'db_A027'
|
|
}
|
|
|
|
TABLES = {}
|
|
TABLES['orders'] = (
|
|
"CREATE TABLE `orders` ("
|
|
" `id` int NOT NULL AUTO_INCREMENT,"
|
|
" `emp_id` varchar(10) NOT NULL,"
|
|
" `emp_name` varchar(10) NOT NULL,"
|
|
" `menu_item_id` int NOT NULL,"
|
|
" `main_course` int NOT NULL,"
|
|
" `order_date` date NOT NULL,"
|
|
" `order_qty` int NOT NULL,"
|
|
" PRIMARY KEY (`id`)"
|
|
") ENGINE=InnoDB")
|
|
|
|
try:
|
|
cnx = mysql.connector.connect(**config)
|
|
cursor = cnx.cursor()
|
|
|
|
# Drop the table if it exists
|
|
cursor.execute("DROP TABLE IF EXISTS orders")
|
|
print("資料表 'orders' 已刪除 (如果存在的話)。")
|
|
|
|
# Create the table
|
|
table_description = TABLES['orders']
|
|
cursor.execute(table_description)
|
|
print("資料表 'orders' 建立成功!")
|
|
|
|
except mysql.connector.Error as err:
|
|
print(f"資料庫錯誤: {err}")
|
|
finally:
|
|
if 'cursor' in locals() and cursor:
|
|
cursor.close()
|
|
if 'cnx' in locals() and cnx.is_connected():
|
|
cnx.close()
|