Domanda

I have a script that will load a file to my mysql db:

for file in files

do

source=$file
gunzip $source
source1=${file%*.*} #remove the gz extension

#connect to database
mysql --host=localhost --user=user --password=password mydb << EOF
LOAD DATA LOCAL INFILE $source1 INTO TABLE mytable;
EOF

done

I keep on getting this error:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'file.txt INTO TABLE mytable' at line 1

Is there a problem with my script? Please help. Thanks!

È stato utile?

Soluzione

The file name must be enclosed into single quotes, as required by the syntax of the LOAD DATA FILE command.

Hence you could try something like (untested):

for file in files
do

    source=$file
    gunzip $source
    source1=${file%*.*} #remove the gz extension

    #connect to database
    mysql --host=localhost --user=user --password=password mydb << EOF
    LOAD DATA LOCAL INFILE '$source1' INTO TABLE mytable;
EOF

done
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top