I want to make a bash script that connects to my MySQL server and inserts some valuse from a txt file. I have written this down:
#!/bin/bash
echo "INSERT INTO test (IP,MAC,SERVER) VALUES ('cat test.txt');" | mysql -uroot -ptest test;
but I'm recieving the following error:
ERROR 1136 (21S01) at line 1: Column count doesn't match value count at row 1
I suppose the error is in my txt file, but I've tried many variations and still no hope of success.
My txt file looks like this:
10.16.54.29 00:f8:e5:33:22:3f marsara
Assuming you have many rows to add, you probably need
LOAD DATA INFILE
statement, notINSERT
. The source file has to be on the server, but it seems to be the case here.Something like that:
LOAD DATA INFILE
has many options as you will discover by reading the doc.I use this and it works:
or select the database first
or copy the whole SQL into the clipboard and paste it with
Try this one:
This way you streaming the file read as well the mysql comand execution.
You are trying to insert the value "cat test.txt" as a String in the database in an INSERT statement that requires 3 parameters (IP,MAC and SERVER) so this is why you get this error message.
You need to read the text file first and extract the IP, MAC and Server values and then use these in the query that would look like this once filled :