Insert data into MySQL database using batch

336 Views Asked by At

I'm encountering difficulties when trying to insert data into my MySQL database using a batch script. Here's the script I'm currently using:

set query=INSERT INTO `my_tbl` (`age` ,`birthdate`) VALUES (15, NOW());
C:\wamp64\bin\mysql\mysql5.7.36\bin\mysql.exe -h localhost --user=root  --password=SECRET -e "%query%" my_db 

However, when I execute the script, I receive the following error message: VALUES was unexpected.

In addition to resolving the error, I would like to replace the static value of 15 with a variable.

The birthdate column is of timestamp data type, while the age column is of integer data type.

1

There are 1 best solutions below

0
Sadek Mehri On

I added a variable called age and set it to 15 then i use it after values keyword. I hope it works.

set age=15
set query=INSERT INTO `my_tbl` (`age`, `birthdate`) VALUES (%age%, NOW())

C:\wamp64\bin\mysql\mysql5.7.36\bin\mysql.exe -h localhost --user=root --password=SECRET -e "%query%" my_db

Or if you want to align the command simply type

C:\wamp64\bin\mysql\mysql5.7.36\bin\mysql.exe -h localhost --user=root --password=SECRET -e "SET @age=15; INSERT INTO my_tbl (age, birthdate) VALUES (@age, NOW());" my_db