MySQL Loading Data Error - mysql

LOAD DATA
LOCAL INFILE "file.txt"
REPLACE INTO TABLE file
FIELDS TERMINATED BY '|'
(attribute1, attribute2)
LOAD DATA
LOCAL INFILE "file2.txt"
REPLACE INTO TABLE file2
FIELDS TERMINATED BY '|'
(attribute3, attribute4)
I have a single text file composed of several of these "LOAD DATA" commands. I receive an error message saying line 6, or the start of the 2nd command, is not proper syntax. And if I try to introduce a "lines terminated by '\n'" code, it says it is not allowed with my mysql version.

You should add a ';' at the end of each load statement.
LOAD DATA
LOCAL INFILE "file.txt"
REPLACE INTO TABLE file
FIELDS TERMINATED BY '|'
(attribute1, attribute2);
LOAD DATA
LOCAL INFILE "file2.txt"
REPLACE INTO TABLE file2
FIELDS TERMINATED BY '|'
(attribute3, attribute4);
See also ERROR 1148: The used command is not allowed with this MySQL version
You can specify that as an additional option when setting up your client connection:
mysql -u myuser -p --local-infile somedatabase

Related

MySQL - LOAD DATA LOCAL INFILE shell script not working

can someone please assist, I am new to mysql, and i have noticed that "LOAD DATA LOCAL INFILE" does not work in mysql event scheduler to update my databases from a normal .csv
So I'm trying to setup a "cron job" in linux to run a shell script to do the LOAD DATA INFILE to my databases, but im getting errors on the following shell script, please help correct it, see my script layout below...
#!/bin/bash
mysql -u root -p xxxxxxx testdb --local_infile=1 -e"LOAD DATA LOCAL INFILE '/mnt/mysqldb/mysqldb-new/mysql/CK-BATCH-FTP/Acelity/activity.csv'
INTO TABLE acelity_activity
FIELDS TERMINATED BY ';'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Thank you for helping me
You must enclose the Double quotes in ENCLOSED BY and i think that you have a finalizing Double quote in you original code
#!/bin/bash
mysql -u root -p xxxxxxx testdb --local_infile=1 -e"LOAD DATA LOCAL INFILE '/mnt/mysqldb/mysqldb-new/mysql/CK-BATCH-FTP/Acelity/activity.csv'
INTO TABLE acelity_activity
FIELDS TERMINATED BY ';'
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;"
And you should test the command, in wokbench or phpmyadmin
Thanks for the help #nbk
So i modified the script above a little, and the following worked for me: (I removed the db name in the mysql syntax and add it in the query)
mysql -u root -pxxxxxxx -e"LOAD DATA LOCAL INFILE '/mnt/mysqldb/mysqldb-new/mysql/CK-BATCH-FTP/File/XXXX.csv'
INTO TABLE <DB NAME>.<TABLE NAME>
FIELDS TERMINATED BY ';'
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;"
If you are using Mysql8 then this will help you.
mysql -udbuser -pXXXXX -h host-pc test1 --local_infile=1 -e "LOAD DATA LOCAL INFILE '/home/abc/data.csv' INTO TABLE tempTable FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' (col1, col2);";
Make sure there is no space in username and password i.e. it should be like this as mentioned in the above command.
Also ENCLOSED BY needs to write in the mentioned format only.

How to export mysql table from the terminal

I'm trying to bypass the -secure-file-priv option that MySQL has enabled to export a table into a .csv file.
I've been running SELECT * FROM final_table INTO OUTFILE 'test.csv' FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"' LINES TERMINATED BY '\r\n'; into the terminal, but it has been returning The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
Previously, I bypassed this error when importing files by running LOAD DATA LOCAL INFILE instead of LOAD DATA INFILE which had the same error and was wondering if there was a way to do it with exporting as well.
Thanks!
Answering my own question since I found a solution that might help out other people.
mysql -u root -p [database] -e '[SELECT * FROM TABLE_NAME]' > data_export.csv
got me exactly what I needed without disabling secure-file-priv!

load data local infile - command not found

As part of a Linux-script, i tried to insert data from a file into a table.
If I try to use the following SQL statement I get this answer from the Server:
./load_test.sh: line 28: -h$SERVER: command not found.
What does it mean?
$MYSQL -h$SERVER -P3306 -uroot -p $DB -e "load data local infile '/var/lib/mysql/scripts/load_test/test.del' INTO TABLE TAB_SNAP_GET_DB_V97 FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n'"

How to find the rows uploaded via Load Data Infile command

I am uploading a csv file via the LOAD DATA INFILE command in a bash script.
I need to find out how many lines of data was uploaded. I need to know this to compare with the number of lines in the csv.
This is to verify that all the data mentioned in the CSV file was uploaded.
I am using a bash script to do this upload as this will excuted on bi-hourly everyday.
How can I get the number of rows uploaded.
mysql -u $_db_user -p$_db_password -D $_db --local-infile=1 <<EOF
LOAD DATA LOCAL INFILE '$_csv_directory/TempImport.csv'
into table TempImport
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n';
EOF

Bash Script for Load Data Infile MySQL

So i'm trying to create a script that I can run that will do a batch import of csv files into a table.
I'm having trouble getting the script to work.
Here is the script i'm running:
#!/bin/bash
for f in *.csv
do
"/opt/lampp/bin/mysql -e use test -e LOAD DATA LOCAL INFILE ("$f") INTO TABLE temp_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (DATE, TIME, SITE_NAME, SITE_IP, TOTAL_TALKTIME, EDGE_UL_BYTES, EDGE_DL_BYTES);"
done
When I run the script I receive the following error message:
./script.sh: line 5: unexpected EOF while looking for matching `''
./script.sh: line 7: syntax error: unexpected end of file
The load data local infile command works fine directly in mysql.
When you want to use literal double quotes in double quoted strings, escape them with \". Since mysql doesn't care about line feeds, you can also break the line to make it more readable:
#!/bin/bash
for f in *.csv
do
/opt/lampp/bin/mysql -e "use test" -e "
LOAD DATA LOCAL INFILE '$f'
INTO TABLE temp_table
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(DATE, TIME, SITE_NAME, SITE_IP, TOTAL_TALKTIME,
EDGE_UL_BYTES, EDGE_DL_BYTES);"
done
mysql -u<username> -p<password> -h<hostname> <db_name> --local_infile=1 -e "use <db_name>" -e"LOAD DATA LOCAL INFILE '<path/file_name>'
IGNORE INTO TABLE <table_name>
FIELDS TERMINATED BY '\t'
OPTIONALLY ENCLOSED BY '\"'"