0
votes

I have a database on my localhost, and I need to put data into MySQL database using batch script. Is this possible? Those values are in .txt files But this doesn't work.

Here is my whole script:

@echo on
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /f "tokens=*" %%A in (C:\Users\Diana\Desktop\names.txt) do (
    SET /A vidx=!vidx! + 1
    snmpwalk -v 2c -c root %%A .1.3.6.1.2.1.25.3.3.1.2 > C:\Users\Diana\Desktop\cpu.txt
    snmpwalk -v 2c -c root %%A .1.3.6.1.2.1.25.5.1.1.2 > C:\Users\Diana\Desktop\ramvid.txt
    snmpwalk -v 2c -c root %%A .1.3.6.1.2.1.25.2.2 > C:\Users\Diana\Desktop\ram.txt
)

for /f "tokens=4" %%B IN (C:\Users\Diana\Desktop\ram.txt) DO echo %%B >> C:\Users\Diana\Desktop\ramfiltruotas.txt
for /f "tokens=4" %%B IN (C:\Users\Diana\Desktop\cpu.txt) DO echo %%B >> C:\Users\Diana\Desktop\cpufiltruotas.txt

for /f "tokens=4" %%a in (C:\Users\Diana\Desktop\ramvid.txt) do set /a used+=%%a 
echo !used! > C:\Users\Diana\Desktop\naujas.txt

for /f "tokens=4" %%c in (C:\Users\Diana\Desktop\ram.txt) do set /a total=%%c
set /a ramai=(%used%*100)/%total%
echo %ramai% > C:\Users\Diana\Desktop\naujas2.txt

for /f "tokens=4" %%d in (C:\Users\Diana\Desktop\cpu.txt) do set /a e+=%%d
set /a cpu=(%e%/4)
echo %cpu% > C:\Users\Diana\Desktop\naujas3.txt

mysql --host=localhost --user=root --password= --database=database <
    INSERT INTO server (cpu, ram)
    WHERE ('server_name' LIKE '192.168.95.139'
    VALUES (%cpu%,%ramai%)
1
What exactly are you trying to do here? I mean the functional requirement of your script?Mark
I have a .txt file, in that file i have one number - CPU load. The value %a% is that numbet because i have calculated it before in the same script, i just didn't posted the beginning of the script. I need to put that number into my database that i could see it in my webpage.user2425381
It seems, you don't want an INSERT, but an UPDATE ?Eugen Rieck
So you are trying to compute the CPU usage as you run the batch script and put it in your database am I right?Mark
Eugen, UPDATE doesn't work either, something is wrong with the syntax.user2425381

1 Answers

0
votes

I just woke up and didn't have coffee yet, but... the way I see it the where clause in your insert statement makes no sense. Try:

INSERT INTO server (cpu, ram , server_name )
VALUES (%cpu%,%ramai%,'192.168.95.139')

instead. This assumes that %cpu% and %ramai% will be replaced with the correct values and that cpu and ram are numeric types in the database. If these are string types, add single quotes, eg ... VALUES('%cpu%','%ramai%','192.168.23.139')

if you want to update existing rows instead of inserting new ones, it would be something like

UPDATE server 
SET cpu=%cpu%, ram=%ramai%
WHERE server_name = '192.168.95.139'