77 lines
2.1 KiB
Batchfile
77 lines
2.1 KiB
Batchfile
@echo off
|
|
setlocal enabledelayedexpansion
|
|
chcp 65001 >nul
|
|
title Import Data Into MySQL
|
|
|
|
:: MySQL connection configuration
|
|
set MYSQL_HOST=127.0.0.1
|
|
set MYSQL_PORT=33306
|
|
set MYSQL_USER=root
|
|
set MYSQL_PASS=Root@2025
|
|
set MYSQL_EXE=mysql.exe
|
|
|
|
:: Script directory
|
|
set SCRIPTS_DIR=script
|
|
|
|
:: 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
|
|
for /d %%D in ("%SCRIPTS_DIR%\*") do (
|
|
set "folder=%%~nxD"
|
|
echo Processing database: !folder!
|
|
|
|
:: 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
|
|
)
|
|
|
|
:: Process all .sql files in the current folder
|
|
for %%F in ("%%D\*.sql") do (
|
|
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
|
|
)
|
|
)
|
|
echo Completed processing database: !folder!
|
|
echo ----------------------------------------
|
|
)
|
|
|
|
echo All SQL files imported successfully!
|
|
pause |