I am trying to import a csv file into mysql database using mysql terminal, nothing seems to work I either get error 1064 or nothing happens after I hit enter.
So far I have tried:
mysql -uusername -ppassword dbname -e "LOAD DATA INFILE 'C:/Users/Administrator/Downloads/file.csv' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' (field1, field2, field3)";
which does nothing if I hit enter without the ; at the end, and with the ; I get error 1064 incorrect syntax.
I have also tried:
mysql -u username -p password -h localhost dbname
use dbname
LOAD DATA INFILE 'C:/Users/Administrator/Downloads/file.csv' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' (field1, field2, field3);
is this correct?
I am using the statements below, and this is the error message I am receiving:
mysql->mysql -u username -ppassword
->use database
->LOAD DATA LOCAL INFILE 'C:/datafeed.csv'
->INTO TABLE table_name
->FIELDS TERMINATED BY ','
->LINES TERMINATED BY '\n'
->(field1, field2, field3);
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 'mysql -u username -ppassword use database LOAD DATA LOCAL INFILE 'C:/datafe' at line 1
The syntax for the LOAD DATA statement itself looks correct...
it looks suitable if the MySQL server is installed on a Windows machine, and the specified file exists and is readable on the server. (There's no LOCAL keyword before INFILE, so that's saying that the file is on the server machine, not the client machine.)
The first thing you show you have tried, I think the double quote character within the LOAD DATA statement is actually terminating the string, that is, it's being "matched" to the double quote before the LOAD DATA.
The fact you are reporting that you are getting a 1064 error makes it appear you are able to connect to the MySQL server.
The form I use for running the mysql command line client on the server machine:
mysql -u user -ppassword database
(Note that there's no space between -p and the password.)
I'm a little confused, whether the MySQL server is actually running on a Windows machine, and if you're running in a CMD.EXE window... there's not enough information provided for me to determine that.
Related
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!
I am trying to run this query
LOAD DATA LOCAL INFILE "C:/myfile.txt"
IGNORE INTO TABLE mydb.mytable
FIELDS TERMINATED BY ';'
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
If I run it in MySQL Workbench (v8.0.16) I get an error
Error Code: 1148. The used command is not allowed with this MySQL version
However if I run it in SQLYog (Professional v12.09) it works fine.
I am running both bits of code using the same computer, with the same user, on the same database running on the same server.
That server has local_infile = 1
As far as I can tell the only difference in these queries is the SQL Client being used.
My assumption is that it is the connection string / settings that is different between the two as a default, however I can't find any documentation on this.
Why would this be and how can I fix it?
Thanks
In linux you have to start mysql client in command line as.
mysql -u root -ppassword --local-infile=1
you should also set local_infile = 1 in configuration file
I cant seem to use community server(or workbench for that matter) to use the Load local data infile command for csv. it always says cant use this command its not allowed on this version.
Ps I tried setting the local_infile global variable as 1 in the client cmd prompt but it still gives the same issue.Also i cant seem to give the parameters directly at start since the client opens on its own.(I dont have simple mysql command on windows only community server)
Why you want to use load local data infile command in Workbench? It already provides the option for table data import wizard.
Also, for the particular error, I tried following code and it worked for me:
(Before connecting to MySQL database type following)
mysql -u username -p --local-infile databasename
Enter Password:
Now, try using the database and importing the table.
MySQL> use databasename;
MySQL> load data local infile 'filename/filelocation.csv' into table tablename
-> fields terminated by ','
-> enclosed by '\r\n'
-> ignore 1 lines
-> (col1, col2, col3...);
I am unable to load the data from file to a MySQL table.
Where else should I keep the file?
File is present as shown below.
notroot#ubuntu:~/lab/data$ ls
txns
notroot#ubuntu:~/lab/data$ pwd
/home/notroot/lab/data
notroot#ubuntu:~/lab/data$ mysql -u root -p
mysql> load data infile '/home/notroot/lab/data/txns' into table trans fields terminated by ',' lines terminated by '\n';
ERROR 29 (HY000): File '/home/notroot/lab/data/txns' not found (Errcode: 13)
mysql> load data local infile '/home/notroot/lab/data/txns' into table trans fields terminated by ',' lines terminated by '\n';
ERROR 1148 (42000): The used command is not allowed with this MySQL version
mysql>
Use the --local-infile parameter when running mysql
mysql --local-infile -u root -p
Is it possible to insert a CSV file into MySQL using a shell script in Ubuntu?
Here's what I tried :
mysql -uroot -proot mysfdb < /home/sf/data.csv
But I am given an 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
Here's a sample content from the CSV file:
showinventory_SST312V8N4615041313_1366009574txt_,800-200002.A0,00007985
Any ideas?
Maksym Polshcha's answer is correct but is only missing a few things. Apparently, since it's a local file, I have to declare it as a local file in the mysql command. The final command should be something like this:
mysql -uroot -proot --local_infile=1 3parsfdb -e "LOAD DATA LOCAL INFILE '/logfiles/Bat_res.csv' INTO TABLE Bat_res FIELDS TERMINATED BY ','"
Also I made sure that the /logfiles directory and the Bat_res.csv are world readable.
Thank you for the great answers.
Try this:
mysql -uroot -proot mysfdb -e "LOAD DATA INFILE '/home/sf/data.csv' INTO TABLE mytable"
where mytable is your table for the data. If you have non-standard field/line separators in your CSV file use FIELDS TERMINATED BY and LINES TERMINATED BY
See http://dev.mysql.com/doc/refman/5.1/en/load-data.html
I used this and it worked.
Login in mysql using `mysql -uroot -ppassword --local-infile`
Then in terminal:
LOAD DATA LOCAL INFILE '.csv path' INTO TABLE table_name FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
Open Ubuntu Terminal and just run the following command
# mysql -u admin -p --local_infile=1 DATABASE_NAME -e "LOAD DATA LOCAL INFILE 'students.csv' INTO TABLE TABLE_NAME FIELDS TERMINATED BY ',' enclosed by '\"'"
Here,
DATABASE_NAME = The name of your database
students.csv = The CSV file directory, that you want to upload in the database
TABLE_NAME = in which table you want to upload your data
admin = Database user name
After run this command system asked for the password of the admin user.
Write the password and Enjoy.