I have a local file structured as follows;
E312D72A2D671437F87446170460320B7F0B53CE:1
E31A0888C42FDE105EA887F2EC8DF1ABF5DBFAA0:1
E326561AE42192053CC09D5EA15DE54230AE4510:1
I want to load this file into a table. The table has 3 columns col1,col2, and col3. I'm using the syntax from dev-mysql as;
LOAD DATA LOCAL INFILE 'input.txt'
INTO table myTable(col1,col2) FIELDS TERMINATED BY ':';
It gives an error as
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 ':'' at line 1
What is the reason for this error? What is the correct syntax?
And, yes my file has two fields and I want to insert them into a table with 3 columns, the last column can be NULL.
mysql --version
mysql Ver 8.0.23-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
You should write it this way:
LOAD DATA LOCAL INFILE 'input.txt'
INTO table myTable FIELDS TERMINATED BY ':' (col1,col2);
Related
I migrated my website to a new server and when importing my database I got an error.
Old server: Mysql 5.5
New SERVER: Mysql 8.0
Here is the import error
[LOAD DATA LOCAL INFILE '/home/xxxx/domaine.com/upload/merchant/13/im_product_ean.sql' INTO TABLE table.PC7_PRODUCT_EAN_ALIAS FIELDS TERMINATED BY '|' ENCLOSED BY ''' LINES TERMINATED BY ' ' (ID, PRODUCT_ID, EAN)] - 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 '-table.PC7_PRODUCT_EAN_ALIAS FIELDS TERMINATED BY '|' ENCLOSED BY ''' LINES TE' at line 1 -
The products must import and update in the database with the cronjob
'mysql> LOAD DATA LOCAL INFILE 'students.csv'
-> INTO TABLE students
-> FIELD TERMINATED BY ',' ENCLOSED BY '"';`
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 'FIELD TERMINATED BY ',' ENCLOSED BY '"'' at line 3.
I'm not sure where my error is occuring. Can someone help me? :(
I have create a shell script to automate my database migration from host to host, that extracts my Multi WordPress site tables into separated sql files, so I can decide what site I will migrate each time.
So in my shell script I have the following code:
schema="internet-safe"
PROJECT_FOLDER_NAME="internet-safe"
vagrant_export_folder="/var/www/projects/${PROJECT_FOLDER_NAME}/database/tmp"
query="mysql -uroot -proot -e \"SELECT blog_id AS ID, path AS Slug FROM ${schema}.wp_blogs "
query="$query INTO OUTFILE '$vagrant_export_folder/blogs.csv' "
query="$query FIELDS TERMINATED BY ',' "
query="$query ENCLOSED BY '\\\"' "
query="$query LINES TERMINATED BY '\n' \" > /dev/null"
vagrant ssh --command "cd $vagrant_export_folder && $query"
But by executing this script I get the following 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 '-safe.wp_blogs INTO OUTFILE '/var/www/projects/internet-safe/database/tmp/blogs' at line 1
Connection to 127.0.0.1 closed.
An the problem is the dash in the schema name internet-safe. Is there a way to correct this issue ?
I know that there is an option, to just rename the schema, but, unfortunately I run on unstable vagrant, on xUbuntu, that has destroy my database several times, while I try to provision modifications in my databases.
I also have try the following combinations but I get error messages like the following:
`${schema}`.`wp_blogs`
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 'INTO OUTFILE '/var/www/projects/internet-safe/database/tmp/blogs.csv' FIELDS TE' at line 1
`${schema}`.wp_blogs
ERROR 1046 (3D000) at line 1: No database selected
`${schema}.wp_blogs`
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 'INTO OUTFILE '/var/www/projects/internet-safe/database/tmp/blogs.csv' FIELDS TE' at line 1
Best option would be of course to simply rename your database.
Usually when special characters appear in schema/table/column names, you can fix this with backticks. Since you're trying to run a shell script, it will interpret anything within backticks as a command, unfortunately.
In MySQL you can set the sql_mode "ANSI_QUOTES". Do so as administrator/root (in MySQL) with
mysql> SET GLOBAL sql_mode="ANSI_QUOTES";
Check first, if you have any modes set already with
mysql> SHOW VARIABLES LIKE 'sql_mode';
If there are already modes set, include those in the SET GLOBAL sql_mode statement (separated by ,).
You can also set this option in your my.cnf file, so that this mode is applied every time you start your MySQL server.
The effect is, that " is also allowed as identifier quote character. But be careful, that you don't use " in queries to specify any text, use single-quotes there instead.
read more about it here
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.
The below query works
SELECT *
FROM some_table
LIMIT 1
INTO outfile 'some_file.txt';
but how would you write the result of this statement to file
SHOW ENGINE INNODB STATUS INTO OUTFILE 'some_file.txt'
or any other show statements, getting error using into outfile?
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 'INTO OUTFILE "some_file.txt"' at line 1
You CANNOT. INTO OUTFILE can be used only with SELECT STATEMENT... hence you're getting 1064 (syntax-error). Please refer to the documentation here: https://dev.mysql.com/doc/refman/5.1/en/select-into.html
It always says-"SELECT ... INTO OUTFILE..."
You need to use console in order to get output of SHOW commands or anything apart of 'SELECT' statement. Here's how you do it:
Follow the syntax below:
mysql --user root --password=sunny -e"show databases">"D:\\test\\OutFile.txt";
Above statement will give your all the databases in outfile.
mysql --user root --password=sunny --database=mydb -e"describe myTable">"D:\\test\\OutFile.txt";
This will output the description of your table.
HTH !