setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 选择数据库 $pdo->exec("USE `{$database}`"); echo "开始创建物流追踪表...\n\n"; // 读取SQL文件 $sql = file_get_contents(__DIR__ . '/database/migrations/create_express_tracking_tables.sql'); // 分割SQL语句 $statements = array_filter( array_map('trim', explode(';', $sql)), function($stmt) { return !empty($stmt) && !preg_match('/^--/', $stmt) && !preg_match('/^\/\*/', $stmt); } ); $success = 0; $failed = 0; foreach ($statements as $statement) { try { $pdo->exec($statement); // 提取表名 if (preg_match('/CREATE TABLE.*?`(\w+)`/i', $statement, $matches)) { echo "✓ 创建表: {$matches[1]}\n"; $success++; } } catch (PDOException $e) { if (strpos($e->getMessage(), 'already exists') !== false) { if (preg_match('/CREATE TABLE.*?`(\w+)`/i', $statement, $matches)) { echo "- 表已存在: {$matches[1]}\n"; } } else { echo "✗ 错误: {$e->getMessage()}\n"; $failed++; } } } echo "\n========================================\n"; echo "安装完成!\n"; echo "========================================\n"; echo "成功: {$success}\n"; echo "失败: {$failed}\n"; echo "\n"; echo "下一步:\n"; echo " php think express:sync # 同步现有快递单号\n"; echo " php think express:auto-update # 测试自动更新\n"; echo "\n";