WindowsStarter/base/Resources/mysql/import-data-into-mysql.bat

77 lines
2.1 KiB
Batchfile
Raw Normal View History

2025-08-14 18:10:10 +08:00
@echo off
setlocal enabledelayedexpansion
2025-08-17 02:18:33 +08:00
chcp 65001 >nul
2025-08-17 04:31:42 +08:00
title Import Data Into MySQL
2025-08-14 18:10:10 +08:00
2025-08-17 04:31:42 +08:00
:: MySQL connection configuration
2025-08-14 18:10:10 +08:00
set MYSQL_HOST=127.0.0.1
set MYSQL_PORT=33306
set MYSQL_USER=root
set MYSQL_PASS=Root@2025
2025-08-17 04:31:42 +08:00
set MYSQL_EXE=mysql.exe
2025-08-14 18:10:10 +08:00
2025-08-17 04:31:42 +08:00
:: Script directory
2025-08-14 18:10:10 +08:00
set SCRIPTS_DIR=script
2025-08-17 04:31:42 +08:00
:: 1. Check if mysql.exe exists
where %MYSQL_EXE% >nul 2>&1
if %errorLevel% neq 0 (
echo Error: mysql.exe not found in PATH
pause
exit /b 1
)
:: 2. Check if scripts directory exists
if not exist "%SCRIPTS_DIR%" (
echo Error: Scripts directory not found at %SCRIPTS_DIR%
pause
exit /b 1
)
:: 3. Verify MySQL connection
echo Testing MySQL connection...
%MYSQL_EXE% -h %MYSQL_HOST% -P %MYSQL_PORT% -u %MYSQL_USER% -p%MYSQL_PASS% -e "SELECT 1" >nul 2>&1
if %errorLevel% neq 0 (
echo Error: Failed to connect to MySQL server
echo Please verify your connection parameters:
echo Host: %MYSQL_HOST%
echo Port: %MYSQL_PORT%
echo User: %MYSQL_USER%
pause
exit /b 1
)
echo MySQL connection successful. Starting import process...
:: Process all folders in the scripts directory
2025-08-14 18:10:10 +08:00
for /d %%D in ("%SCRIPTS_DIR%\*") do (
set "folder=%%~nxD"
2025-08-17 04:31:42 +08:00
echo Processing database: !folder!
2025-08-14 18:10:10 +08:00
2025-08-17 04:31:42 +08:00
:: Create database (if not exists)
echo Creating database '!folder!' if not exists...
%MYSQL_EXE% -h %MYSQL_HOST% -P %MYSQL_PORT% -u %MYSQL_USER% -p%MYSQL_PASS% -e "CREATE DATABASE IF NOT EXISTS `!folder!`;"
if %errorLevel% neq 0 (
echo Error: Failed to create database '!folder!'
pause
exit /b 1
)
2025-08-14 18:10:10 +08:00
2025-08-17 04:31:42 +08:00
:: Process all .sql files in the current folder
2025-08-14 18:10:10 +08:00
for %%F in ("%%D\*.sql") do (
2025-08-17 04:31:42 +08:00
echo Importing file: %%~nxF into database !folder!
%MYSQL_EXE% -h %MYSQL_HOST% -P %MYSQL_PORT% -u %MYSQL_USER% -p%MYSQL_PASS% !folder! < "%%F"
if %errorLevel% neq 0 (
echo Error: Failed to import file %%~nxF
pause
exit /b 1
)
2025-08-14 18:10:10 +08:00
)
2025-08-17 04:31:42 +08:00
echo Completed processing database: !folder!
echo ----------------------------------------
2025-08-14 18:10:10 +08:00
)
2025-08-17 04:31:42 +08:00
echo All SQL files imported successfully!
2025-08-14 18:10:10 +08:00
pause