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""")
Related
I'm getting #1064 - You have an error in your SQL syntax; in the following MySQL query.
LOAD DATA LOCAL INFILE 'path-to-file.csv'
INTO TABLE ls_missing_products
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\\r'
ROWS (ProductID, status)
SET status = 0;
Following is the full error message i'm getting.
#1064 - 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 'ROWS (ProductID, status) SET status = 0' at line 1
ROWS is part of IGNORE keyword in LOAD DATA either add IGNORE or remove ROWS. https://dev.mysql.com/doc/refman/5.7/en/load-data.html
I have tried the suggestion in the question below but I still have syntax errors.
How to LOAD DATA INFILE in mysql with first col being Auto Increment?
create table db.test
(ai_id int(11) auto_increment primary key,
field varchar(5))
LOAD DATA LOCAL INFILE 'C:\\Users\\nick\\Desktop\\test\\book1.csv'
INTO TABLE db.test
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(field)
SET ai_id = NULL
IGNORE 1 lines;
I am having trouble reconciling this seemingly very simple syntax error, any assistance greatly appreciated!
EDIT:
error code: 1064: You have error in SQL syntax; check syntax around 'ignore 1 lines' line 8.
datasource is a csv with one column "field" with five rows "one"-"five"(all five rows are characters not int)
This syntax is correct, I tested its working(in MySQL 5.6). please verify your input file.
The following works. It appears the order of commands is what threw it off.
LOAD DATA LOCAL INFILE 'C:\\Users\\nshatz\\Desktop\\test\\book1.csv'
INTO TABLE db.test
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 lines
(field);
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);
I am trying to load a few thousand records into my MySQL db from a tab delimited text file, but I am getting the error message:
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 '\t' ENCLOSED BY '" '
LINES TERMINATED BY '\n'' at line 2
My command is:
LOAD DATA INFILE 'records.txt' INTO TABLE records (vendor, title, id, part, project,
description, machine, shelf, compartment, checkout)
FIELDS TERMINATED BY '\t' ENCLOSED BY '"'
LINES TERMINATED BY '\n';
I have tried different options such as OPTIONALLY ENCLOSED BY '"', LINES TERMINATED BY '\r\n', and adding a space after the quotation mark in ENCLOSED BY '" ', but I still get the error message above.
Where am I going wrong?
I am not very used to this command but I would say that the right query is :
LOAD DATA INFILE 'records.txt' INTO TABLE records
FIELDS TERMINATED BY '\t' ENCLOSED BY '"'
LINES TERMINATED BY '\n' (vendor, title, id, part, project,
description, machine, shelf, compartment, checkout);
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.