414 lines
19 KiB
Python
414 lines
19 KiB
Python
|
||
|
||
import mysql.connector
|
||
from mysql.connector import errorcode
|
||
|
||
|
||
DB_CONFIG = {
|
||
'host': 'localhost',
|
||
'user': 'root',
|
||
'password': '123456', # 替换为你的 MySQL 密码
|
||
'port': 3306
|
||
}
|
||
|
||
|
||
DATABASE_NAME = 'tgst01'
|
||
|
||
|
||
SQL_SCRIPT = """
|
||
SET NAMES utf8mb4;
|
||
SET FOREIGN_KEY_CHECKS = 0;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for announcements
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `announcements`;
|
||
CREATE TABLE `announcements` (
|
||
`id` int NOT NULL AUTO_INCREMENT,
|
||
`text` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
|
||
`start_time` datetime NOT NULL,
|
||
`end_time` datetime NOT NULL,
|
||
`color` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
|
||
`created_at` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
|
||
PRIMARY KEY (`id`)
|
||
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for coupons
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `coupons`;
|
||
CREATE TABLE `coupons` (
|
||
`coupon_id` int NOT NULL AUTO_INCREMENT,
|
||
`coupon_code` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
|
||
`discount_type` enum('fixed','percentage') CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
|
||
`discount_amount` decimal(10,2) DEFAULT NULL,
|
||
`min_order_amount` decimal(10,2) DEFAULT NULL,
|
||
`valid_from` datetime DEFAULT NULL,
|
||
`valid_to` datetime DEFAULT NULL,
|
||
`is_active` tinyint(1) DEFAULT '1',
|
||
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||
`quantity` int DEFAULT NULL,
|
||
PRIMARY KEY (`coupon_id`),
|
||
UNIQUE KEY `coupon_code` (`coupon_code`)
|
||
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for game_groups
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `game_groups`;
|
||
CREATE TABLE `game_groups` (
|
||
`group_id` int NOT NULL AUTO_INCREMENT,
|
||
`user_id` int NOT NULL,
|
||
`group_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
|
||
`description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
|
||
`start_date` date DEFAULT NULL,
|
||
`start_time` datetime DEFAULT NULL,
|
||
`end_time` datetime DEFAULT NULL,
|
||
`max_members` int DEFAULT NULL,
|
||
`group_status` enum('recruiting','full','completed','cancelled','pause') CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT 'recruiting',
|
||
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||
`play_start_time` datetime DEFAULT NULL,
|
||
`play_end_time` datetime DEFAULT NULL,
|
||
PRIMARY KEY (`group_id`)
|
||
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for game_tables
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `game_tables`;
|
||
CREATE TABLE `game_tables` (
|
||
`table_id` int NOT NULL AUTO_INCREMENT COMMENT '服务器桌号',
|
||
`game_table_number` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '物理桌号',
|
||
`capacity` int NOT NULL COMMENT '承载人数',
|
||
`price` decimal(10,2) NOT NULL COMMENT '价格倍率',
|
||
`table_pricing_strategy_id` int DEFAULT NULL,
|
||
PRIMARY KEY (`table_id`),
|
||
KEY `tables_price` (`table_pricing_strategy_id`),
|
||
CONSTRAINT `tables_price` FOREIGN KEY (`table_pricing_strategy_id`) REFERENCES `table_pricing_strategies` (`strategy_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
|
||
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for game_tags
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `game_tags`;
|
||
CREATE TABLE `game_tags` (
|
||
`game_id` int NOT NULL,
|
||
`tag_id` int NOT NULL,
|
||
PRIMARY KEY (`game_id`,`tag_id`),
|
||
KEY `tag_id` (`tag_id`),
|
||
CONSTRAINT `game_tags_ibfk_1` FOREIGN KEY (`game_id`) REFERENCES `games` (`game_id`),
|
||
CONSTRAINT `game_tags_ibfk_2` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`tag_id`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for games
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `games`;
|
||
CREATE TABLE `games` (
|
||
`game_id` int NOT NULL AUTO_INCREMENT,
|
||
`game_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
|
||
`game_type` int NOT NULL,
|
||
`description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
|
||
`min_players` int DEFAULT NULL,
|
||
`max_players` int DEFAULT NULL,
|
||
`duration` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
|
||
`price` decimal(10,2) DEFAULT NULL,
|
||
`difficulty_level` int DEFAULT NULL,
|
||
`is_available` tinyint(1) DEFAULT '1',
|
||
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||
`quantity` int DEFAULT NULL,
|
||
`photo_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
|
||
`long_description` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
|
||
PRIMARY KEY (`game_id`)
|
||
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for group_members
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `group_members`;
|
||
CREATE TABLE `group_members` (
|
||
`group_member_id` int NOT NULL AUTO_INCREMENT,
|
||
`group_id` int DEFAULT NULL,
|
||
`user_id` int DEFAULT NULL,
|
||
`join_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||
`entry_status` int DEFAULT '1',
|
||
PRIMARY KEY (`group_member_id`),
|
||
KEY `group_id` (`group_id`),
|
||
CONSTRAINT `group_members_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `game_groups` (`group_id`)
|
||
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for order_coupons
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `order_coupons`;
|
||
CREATE TABLE `order_coupons` (
|
||
`order_coupon_id` int NOT NULL AUTO_INCREMENT,
|
||
`order_id` int DEFAULT NULL,
|
||
`coupon_id` int DEFAULT NULL,
|
||
PRIMARY KEY (`order_coupon_id`),
|
||
KEY `order_id` (`order_id`),
|
||
KEY `coupon_id` (`coupon_id`),
|
||
CONSTRAINT `order_coupons_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`order_id`),
|
||
CONSTRAINT `order_coupons_ibfk_2` FOREIGN KEY (`coupon_id`) REFERENCES `coupons` (`coupon_id`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for orders
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `orders`;
|
||
CREATE TABLE `orders` (
|
||
`order_id` int NOT NULL COMMENT '订单 id',
|
||
`user_id` int NOT NULL COMMENT '下单用户 id',
|
||
`game_table_id` int NOT NULL COMMENT '占用游戏桌 id',
|
||
`game_id` int DEFAULT NULL COMMENT '使用游戏 id',
|
||
`order_date` date NOT NULL COMMENT '下单时间',
|
||
`start_datetime` datetime NOT NULL COMMENT '订单开始时间',
|
||
`end_datetime` datetime DEFAULT NULL COMMENT '订单结束时间',
|
||
`num_players` int NOT NULL COMMENT '游玩人数',
|
||
`payable_price` decimal(10,2) DEFAULT NULL COMMENT '未优惠价格',
|
||
`order_status` enum('pending','paid','in_progress','completed','cancelled') CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT 'pending' COMMENT '订单状态',
|
||
`payment_method` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '支付平台',
|
||
`coupon_id` int DEFAULT NULL COMMENT '优惠卷 id',
|
||
`used_points` int DEFAULT NULL COMMENT '使用积分',
|
||
`paid_price` decimal(10,2) DEFAULT '0.00' COMMENT '优惠后价格',
|
||
`game_process_time` int DEFAULT NULL COMMENT '游戏时长',
|
||
`settlement_time` datetime DEFAULT NULL,
|
||
`wx_transaction_id` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
|
||
`out_trade_no` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
|
||
`pricing_strategy_id` int NOT NULL COMMENT '绑定的价格策略 ID',
|
||
PRIMARY KEY (`order_id`),
|
||
KEY `user_id` (`user_id`),
|
||
KEY `game_table_id` (`game_table_id`),
|
||
KEY `game_id` (`game_id`),
|
||
KEY `coupon_id` (`coupon_id`),
|
||
KEY `orders_ibfk_4` (`pricing_strategy_id`),
|
||
CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`),
|
||
CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`game_table_id`) REFERENCES `game_tables` (`table_id`),
|
||
CONSTRAINT `orders_ibfk_3` FOREIGN KEY (`game_id`) REFERENCES `games` (`game_id`),
|
||
CONSTRAINT `orders_ibfk_4` FOREIGN KEY (`pricing_strategy_id`) REFERENCES `pricing_strategies` (`strategy_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for player_messages
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `player_messages`;
|
||
CREATE TABLE `player_messages` (
|
||
`message_id` int NOT NULL AUTO_INCREMENT,
|
||
`user_id` int NOT NULL,
|
||
`message_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
|
||
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||
PRIMARY KEY (`message_id`),
|
||
KEY `user_id` (`user_id`),
|
||
CONSTRAINT `player_messages_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE
|
||
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for player_reviews
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `player_reviews`;
|
||
CREATE TABLE `player_reviews` (
|
||
`review_id` int NOT NULL AUTO_INCREMENT,
|
||
`user_id` int NOT NULL,
|
||
`game_id` int NOT NULL,
|
||
`rating` tinyint NOT NULL COMMENT '玩家的打分,范围可以根据实际情况设定,如 1 - 5 星',
|
||
`comment` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin COMMENT '玩家的评论内容,可以为空',
|
||
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||
PRIMARY KEY (`review_id`),
|
||
KEY `user_id` (`user_id`),
|
||
KEY `game_id` (`game_id`),
|
||
CONSTRAINT `player_reviews_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE,
|
||
CONSTRAINT `player_reviews_ibfk_2` FOREIGN KEY (`game_id`) REFERENCES `games` (`game_id`) ON DELETE CASCADE
|
||
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for points_history
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `points_history`;
|
||
CREATE TABLE `points_history` (
|
||
`id` int NOT NULL AUTO_INCREMENT,
|
||
`user_id` int NOT NULL,
|
||
`change_amount` int NOT NULL,
|
||
`reason` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
|
||
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||
PRIMARY KEY (`id`),
|
||
KEY `user_id` (`user_id`),
|
||
CONSTRAINT `points_history_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
|
||
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for pricing_strategies
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `pricing_strategies`;
|
||
CREATE TABLE `pricing_strategies` (
|
||
`strategy_id` int NOT NULL AUTO_INCREMENT COMMENT '价格策略 ID',
|
||
`strategy_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '策略名称',
|
||
`description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '策略描述',
|
||
`segment1_threshold` int NOT NULL COMMENT '第一阶段时间(分钟)',
|
||
`segment1_price` decimal(10,2) NOT NULL COMMENT '第一阶段单价(元/人/分钟)',
|
||
`segment2_threshold` int NOT NULL COMMENT '第二阶段时间(分钟)',
|
||
`segment2_price` decimal(10,2) NOT NULL COMMENT '第二阶段单价(元/人/分钟)',
|
||
`segment3_price` decimal(10,2) NOT NULL COMMENT '第三阶段单价(元/人/分钟)',
|
||
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||
`segment3_threshold` int DEFAULT NULL COMMENT '第三阶段时间上限(分钟,可选)',
|
||
PRIMARY KEY (`strategy_id`)
|
||
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for reviews
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `reviews`;
|
||
CREATE TABLE `reviews` (
|
||
`review_id` int NOT NULL AUTO_INCREMENT,
|
||
`user_id` int DEFAULT NULL,
|
||
`game_id` int DEFAULT NULL,
|
||
`rating` int DEFAULT NULL,
|
||
`comment` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
|
||
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||
PRIMARY KEY (`review_id`),
|
||
KEY `user_id` (`user_id`),
|
||
KEY `game_id` (`game_id`),
|
||
CONSTRAINT `reviews_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`),
|
||
CONSTRAINT `reviews_ibfk_2` FOREIGN KEY (`game_id`) REFERENCES `games` (`game_id`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for table_pricing_strategies
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `table_pricing_strategies`;
|
||
CREATE TABLE `table_pricing_strategies` (
|
||
`strategy_id` int NOT NULL AUTO_INCREMENT COMMENT '桌费策略 ID',
|
||
`strategy_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '策略名称',
|
||
`description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '策略描述',
|
||
`segment1_threshold` int NOT NULL COMMENT '第一阶段时间(分钟)',
|
||
`segment1_price` decimal(10,2) NOT NULL COMMENT '第一阶段单价(元/分钟)',
|
||
`segment2_threshold` int NOT NULL COMMENT '第二阶段时间(分钟)',
|
||
`segment2_price` decimal(10,2) NOT NULL COMMENT '第二阶段单价(元/分钟)',
|
||
`segment3_price` decimal(10,2) NOT NULL COMMENT '第三阶段单价(元/分钟)',
|
||
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||
`segment3_threshold` int DEFAULT NULL COMMENT '第三阶段时间上限(分钟,可选)',
|
||
PRIMARY KEY (`strategy_id`)
|
||
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for tags
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `tags`;
|
||
CREATE TABLE `tags` (
|
||
`tag_id` int NOT NULL AUTO_INCREMENT,
|
||
`tag_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
|
||
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||
PRIMARY KEY (`tag_id`),
|
||
UNIQUE KEY `tag_name` (`tag_name`)
|
||
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for user_coupons
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `user_coupons`;
|
||
CREATE TABLE `user_coupons` (
|
||
`user_coupon_id` int NOT NULL AUTO_INCREMENT,
|
||
`user_id` int NOT NULL,
|
||
`coupon_id` int NOT NULL,
|
||
`obtained_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||
`used_at` timestamp NULL DEFAULT NULL,
|
||
`is_used` tinyint(1) DEFAULT '0',
|
||
`valid_from` date DEFAULT NULL,
|
||
`valid_to` date DEFAULT NULL,
|
||
PRIMARY KEY (`user_coupon_id`),
|
||
KEY `coupon_id` (`coupon_id`),
|
||
KEY `idx_user_status` (`user_id`,`is_used`),
|
||
CONSTRAINT `user_coupons_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE,
|
||
CONSTRAINT `user_coupons_ibfk_2` FOREIGN KEY (`coupon_id`) REFERENCES `coupons` (`coupon_id`) ON DELETE CASCADE
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for user_game_rating
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `user_game_rating`;
|
||
CREATE TABLE `user_game_rating` (
|
||
`id` int NOT NULL AUTO_INCREMENT,
|
||
`user_id` int NOT NULL,
|
||
`game_id` int NOT NULL,
|
||
`rating` tinyint(1) NOT NULL COMMENT '拉为 1,踩为 0',
|
||
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||
PRIMARY KEY (`id`),
|
||
KEY `user_id` (`user_id`),
|
||
KEY `game_id` (`game_id`),
|
||
CONSTRAINT `user_game_rating_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE,
|
||
CONSTRAINT `user_game_rating_ibfk_2` FOREIGN KEY (`game_id`) REFERENCES `games` (`game_id`) ON DELETE CASCADE
|
||
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
-- ----------------------------
|
||
-- Table structure for users
|
||
-- ----------------------------
|
||
DROP TABLE IF EXISTS `users`;
|
||
CREATE TABLE `users` (
|
||
`user_id` int NOT NULL AUTO_INCREMENT,
|
||
`username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
|
||
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
|
||
`phone_number` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
|
||
`email` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
|
||
`user_type` enum('player','admin') CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT 'player',
|
||
`gender` enum('male','female','other') CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
|
||
`points` int DEFAULT '0',
|
||
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
||
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||
`wx_openid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
|
||
PRIMARY KEY (`user_id`),
|
||
UNIQUE KEY `phone_number` (`phone_number`)
|
||
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||
|
||
SET FOREIGN_KEY_CHECKS = 1;
|
||
|
||
"""
|
||
|
||
|
||
def create_database_and_tables():
|
||
try:
|
||
# 第一步:先连到 MySQL Server(不指定数据库)
|
||
cnx = mysql.connector.connect(**DB_CONFIG)
|
||
cursor = cnx.cursor()
|
||
# 创建数据库(如果不存在则创建)
|
||
cursor.execute(f"CREATE DATABASE IF NOT EXISTS {DATABASE_NAME} CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;")
|
||
print(f"Database {DATABASE_NAME} created or already exists.")
|
||
cursor.close()
|
||
cnx.close()
|
||
|
||
# 第二步:重新连接到刚创建/已存在的数据库
|
||
cnx = mysql.connector.connect(database=DATABASE_NAME, **DB_CONFIG)
|
||
cursor = cnx.cursor()
|
||
|
||
# 由于脚本中有多条语句,需要手动分割执行
|
||
# 简单做法:按分号分割,然后逐条执行
|
||
# 注意如果脚本中有存储过程/触发器之类带有分号的场景,需要更复杂的处理。
|
||
statements = SQL_SCRIPT.split(';')
|
||
for stmt in statements:
|
||
# 去除前后空格
|
||
stmt = stmt.strip()
|
||
if stmt: # 如果不为空,就执行
|
||
cursor.execute(stmt)
|
||
|
||
cursor.close()
|
||
cnx.close()
|
||
print("All tables created successfully!")
|
||
|
||
except mysql.connector.Error as err:
|
||
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
|
||
print("连接数据库失败:用户名或密码错误。")
|
||
elif err.errno == errorcode.ER_BAD_DB_ERROR:
|
||
print("数据库不存在,且创建失败。")
|
||
else:
|
||
print(err)
|
||
except Exception as e:
|
||
print("执行过程中出现错误:", e)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
create_database_and_tables()
|