LOAD DATA LOCAL INFILE syntax error - mysql

working with MySQL: mysql Ver 14.14 Distrib 5.6.24, for debian-linux-gnu (x86_64) using EditLine wrapper
I am trying to cron a LOAD DATA LOCAL INFILE job to run twice a day. I am having a remarkably difficult time sorting my syntax. Currently, I have this:
#!/bin/bash
mysql --user=dbuser --password="dbuserpassword" --database=DSDB --local-infile --execute "
LOAD DATA LOCAL INFILE '/mnt/hqsccm/TSReport.CSV'
INTO TABLE temptable
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
(#compname,#imgdate,#imgname,#imgsttime,#imgendtime,#tottime,#engname,#engemail) set ComputerName=#compname,ImagingDate=#imgdate,ImageName=#imgname,ImageSTartTime=#imgsttime,ImageEndTime=#imgendtime,TotalTime=#tottime,EngineerName=#engname,EngineerEmail=#engemail;
TRUNCATE HQSCCMmachines;
INSERT INTO HQSCCMmachines (ComputerName,ImagingDate,ImageName,ImageSTartTime,ImageEndTime,TotalTime,EngineerName,EngineerEmail)
SELECT (ComputerName,ImagingDate,ImageName,ImageSTartTime,ImageEndTime,TotalTime,EngineerName,EngineerEmail)
FROM temptable
TRUNCATE temptable;
"
And I keep getting this:
ERROR 1064 (42000) at line 11: 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 'temptable' at line 5
Help?

This should work(if file and table exist)-
LOAD DATA LOCAL INFILE '/mnt/hqsccm/TSReport.CSV'
INTO TABLE temptable
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
(#compname,#imgdate,#imgname,#imgsttime,#imgendtime,#tottime,#engname,#engemail) set ComputerName=#compname,ImagingDate=#imgdate,ImageName=#imgname,ImageSTartTime=#imgsttime,ImageEndTime=#imgendtime,TotalTime=#tottime,EngineerName=#engname,EngineerEmail=#engemail;
TRUNCATE HQSCCMmachines;
INSERT INTO HQSCCMmachines (ComputerName,ImagingDate,ImageName,ImageSTartTime,ImageEndTime,TotalTime,EngineerName,EngineerEmail)
SELECT ComputerName,ImagingDate,ImageName,ImageSTartTime,ImageEndTime,TotalTime,EngineerName,EngineerEmail
FROM temptable;
TRUNCATE temptable;

Related

Python3 cursor.execute correct syntax

Trying to insert some data using python3 and a local csv file - what is wrong with this syntax? python keeps saying
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 ' FIELDS
TERMINATED BY ','' at line 1
cursor.execute("""LOAD DATA LOCAL INFILE '/home/user/mongo_exported_users.csv' INTO TABLE users IGNORE 1 LINES, FIELDS TERMINATED BY ','""")
According to the documentation, there can be no comma before the FIELDS TERMINATED BY clause, and also, the IGNORE # LINES clause must come after the FIELDS TERMINATED BY clause:
cursor.execute("""LOAD DATA LOCAL INFILE '/home/user/mongo_exported_users.csv'
INTO TABLE users FIELDS TERMINATED BY ',' IGNORE 1 LINES""")

How to fix shell bash mysql load query syntax error?

I need a shell script to load data into mysql db. The script is the next:
# !bin/bash
qry="DROP TABLE IF EXISTS tmp_x;
CREATE TEMPORARY TABLE tmp_x AS SELECT * FROM x.y LIMIT 0;
LOAD DATA INFILE 'path/xxx.csv'
INTO TABLE tmp_x
FIELDS TERMINATED BY "\,"
ENCLOSED BY "\""
LINES TERMINATED BY "\\n"
IGNORE 1 ROWS;"
mysql --host=xxx --user=xxx --password=xxx db << EOF
$qry
EOF
I get the following error message:
ERROR 1064 (42000) at line 3: 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 '
ENCLOSED BY "
LINES TERMINATED BY \n
IGNORE 1 ROWS' at line 3
I think it is something to do escaping some character, I tried changing to single quotes but it does not work neither.
I am workin on Ubuntu 18.
Any help will be very grateful.
Try this:
#!/bin/bash
mysql --host=xxx --user=xxx --password=xxx db << EOF
DROP TABLE IF EXISTS tmp_x;
CREATE TEMPORARY TABLE tmp_x AS SELECT * FROM x.y LIMIT 0;
LOAD DATA INFILE 'path/xxx.csv'
INTO TABLE tmp_x
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\\n'
IGNORE 1 ROWS;
EOF
If you really must use a variable, you'll need to play with quoting:
#!/bin/bash
qry="DROP TABLE IF EXISTS tmp_x;
CREATE TEMPORARY TABLE tmp_x AS SELECT * FROM x.y LIMIT 0;
LOAD DATA INFILE 'path/xxx.csv'
INTO TABLE tmp_x
FIELDS TERMINATED BY \",\"
ENCLOSED BY \"\\\"\"
LINES TERMINATED BY \"\\n\"
IGNORE 1 ROWS;"
mysql --host=xxx --user=xxx --password=xxx db << EOF
$qry
EOF
It can be troublesome to use double-quoted strings in your SQL, since you're using double-quotes as the string delimiter in bash. In other words, which is the double-quote that ends the bash string, and which should be treated as a literal double-quote character in the SQL?
To resolve this, use single-quotes for string delimiters in the SQL.
Another issue: There's no need to put a backslash before , for the field terminator.
Another issue: The \n needs another backslash.
Here's what I tried and it seems to work:
qry="DROP TABLE IF EXISTS tmp_x;
CREATE TEMPORARY TABLE tmp_x AS SELECT * FROM x.y LIMIT 0;
LOAD DATA INFILE 'path/xxx.csv'
INTO TABLE tmp_x
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\\\n'
IGNORE 1 ROWS;"
I only printed the query, I haven't tested running it.

using Load data in mysql

I am trying to load some data from a csv file to mysql.
This is on a raspberry pi.
I tried with "--local-infile=1" and without.
pi data > cat test.csv
2014-10-30 08-09-08,1
2014-10-30 08-09-13,2
2014-10-30 08-09-18,3
2014-10-30 08-09-23,4
2014-10-30 08-09-28,5
2014-10-30 08-09-33,6
2014-10-30 08-09-38,7
2014-10-30 08-09-43,8
2014-10-30 08-09-48,9
2014-10-30 08-09-53,10
and this is what I tried:
pi data > mysql --uroot -ppasswd -s solar --local-infile=1
mysql> create table if not exists temp (
-> time TIMESTAMP,
-> voltage SMALLINT UNSIGNED,
-> primary key (time)
-> );
mysql> LOAD DATA INFILE 'test.csv' INTO TABLE 'temp' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (time,voltage);
ERROR 1064 (42000): 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 ''temp' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (time,voltage)' at line 1
mysql> LOAD DATA LOCAL INFILE 'test.csv' INTO TABLE 'temp' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (time,voltage);
ERROR 1064 (42000): 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 ''temp' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (time,voltage)' at line 1
Any help would be appreciated.
Thanks.
You are using the wrong characters around your table name. MySql uses backticks not quotes for field and table names.
LOAD DATA INFILE 'test.csv' INTO TABLE `temp` FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (time,voltage);

BULK INSERT in MYSQL

On MS SQL, I can do bulk insert using the sql command below:
BULK INSERT myDatabase.MyTable FROM 'C:\MyTextFile.txt' WITH FIELDTERMINATOR = ','
Now I want to do the same on MySQL but I can't seem to figure out how this works and what query to use.
In MySQL, the equivalent would be
LOAD DATA INFILE
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
LOAD DATA INFILE 'C:\MyTextFile'
INTO TABLE myDatabase.MyTable
FIELDS TERMINATED BY ','

Error in mysql while importing from csv

This is my command line query.
mysql> load data local infile "c:\\re\\30-11-08.csv"
into table powerdata(Date, DG1, DG2, DG3, Dg4, DG5, ChillerPanel1,
ChillerPanel2, ChillerPanel3, ChillerPanel4,1st_Floor, 2nd_Floor,
3rd_Floor, 4th_Floor, UPS1, UPS2, UPS3, UPS4, UPS5,Server_Power,
Cooling_Power)
fields terminated by ',' lines terminated by '\n'
set Dateformat=str_to_date(Date, '%m/%d/%Y' '%H:%i:%s');
ERROR 1064 (42000): 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 'fields terminated by ',' lines terminated by '\n'set Dateformat=str_to_date(Date' at line 1
I do not know where the error is! Can anyone help me?
I suppose the "set Dateformat=" part is causing the problem. Your column is named "Date" so that part should look like:
set Date = str_to_date(#datevar, 'your format')
Also see the following code sample in the manual:
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, #var1)
SET column2 = #var1/100;
BTW: before MySQL 5.0.3 the SET clause is not supported.