When I run the following code I get an error stating that the file is not found. I am not sure why. I checked the file names and they match.
load data local infile '/Users/blah/Desktop/A.csv'
into table B fields terminated by ','
enclosed by '"' lines terminated by '\n' ;
For security, MySQL only knows about it's directory structure. If it were able to read /Users/... then that would be a security gap.
To resolve, you can copy the A.csv into /var/lib/mysql/[Schema Name]/ and then "load data" that file.
Remember that the file path must be relative to the database server.
Related
In an attempt to export data from MySQL to a .csv file, I execute the following:
SELECT 'BAT_POSITION_IN_SERIES', 'NUMBER_OF_PITCHES', 'PCT_BALLS', 'PCT_CALLED_STRIKE', 'STRIKEOUTS_PER_PITCH', 'WALKS_PER_PITCH', 'HITS_PER_PITCH', 'RUNS_PER_PITCH'
UNION
SELECT *
FROM per_pitch_summary
INTO OUTFILE 'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\per_pitch_summary.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ','
ESCAPED BY '"'
LINES TERMINATED BY '\n' ;
however I am unable to see the Uploads folder as indicated in the file path. It must exist, because when I re-execute the query, I receive an error message indicating the .csv file already exists. Any suggestions? Why would I not be able to see the folder?
I'm completely novice to MySQL, and I'm trying to load some CSV sheets to the server using HeidiSQL -> tools -> Import CSV. But it keeps giving me this error:
Error 1148: The used command is not allowed with this MySQL version".
Is there a way to fix that, or maybe another way to load a CSV?
For query based csv import you can use load data ... to load csv files.
For more info refer here
Example:
To skip the first line which is header in csv you can use ignore 1 lines
load data local infile 'D:std_table.csv' into table local.student
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n'
ignore 1 lines;
For windows based system use \r\n for line termination and for Linux use \n
Side Note:
You could try using MySQL Workbench for MySQL from official site here.
try this one:
LOAD DATA INFILE 'c:/tmp/discounts.csv'
INTO TABLE discounts
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
I am trying to upload a large .csv file into a database (not on a server, just on my local computer only for my use) on MySQL. I have been having the Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement error thrown.
I put my .csv file into the specified 'safe' folder (C:/ProgramData/MySQL/MySQL Server 5.7/Uploads) specified in the 'my' file, but it kept giving me the same error. Every solution I've seen has been using that folder destination and since that doesn't work can anyone help me get around or turn off the secure-file-priv option?
Here is my code in case wanted:
LOAD DATA INFILE 'C:\ProgramData\MySQL\MySQL Server 5.7\Uploads\my-file.csv'
INTO TABLE my-table
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Thank you for any help
From the MySQL documentation for LOAD DATA INFILE:
The file name must be given as a literal string. On Windows, specify backslashes in path names as forward slashes or doubled backslashes. The character_set_filesystem system variable controls the interpretation of the file name.
LOAD DATA INFILE "C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/my-file.csv"
INTO TABLE my-table
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
I had the same issue. Using forward slashes instead of backslashes (even on Windows machine) fixed it for me.
So I am trying to access my servers databse reomtely and have it run commands to export several tables all to individual csv files. So what I have is a commmand line command parameters that look like this:
mysql -h 198.xxx.xxx.xxx -u user-p < file.txt
The contents of file.txt looks like this:
SELECT * FROM log
INTO OUTFILE 'C:\USERS\username\Desktop\log.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
SELECT * FROM permission_types
INTO OUTFILE 'C:\USERS\username\Desktop\permission_types.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
SELECT * FROM personal_info_options
INTO OUTFILE 'C:\USERS\username\Desktop\personal_info_options.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
I am not sure that I have the syntax for this right or if this is even possible I have been doing a bunch of research trying to get examples. People usually tell you concept but never seem to give you the code you need to test them its always something like:
mysql -h localhost -u user-p < somefile and they don't show you contents
of a file for example
I am running windows 7, I installed WAMPServer and it has MYSQL version 5.5.24, which I am access via commandline. I am not sure about the FILEDS TERMINATED BY or the ENCLOSED BY or LINES TERMINATED BY... do I need those at all? Will that actually save to my local machine? I am nervous about running this script I don't want to make a mistake and mess up the database. Also is .txt ok for the script file?
Any help you can give would be great.
I am not sure that I have the syntax for this right or if this is even possible I have been doing a bunch of research trying to get examples.
Your syntax is correct, except that each SELECT statement should be terminated with a semicolon. Note that you will also need to specify the database in which your tables reside—it's easiest to do this as an argument to mysql:
mysql -h 198.xxx.xxx.xxx -u user-p mydb < file.txt
I am not sure about the FILEDS TERMINATED BY or the ENCLOSED BY or LINES TERMINATED BY... do I need those at all?
As documented under SELECT ... INTO Syntax:
Here is an example that produces a file in the comma-separated values (CSV) format used by many programs:
SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;
As explained under LOAD DATA Syntax:
If you specify no FIELDS or LINES clause, the defaults are the same as if you had written this:
FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\'
LINES TERMINATED BY '\n' STARTING BY ''
It goes on to explain:
Conversely, the defaults cause SELECT ... INTO OUTFILE to act as follows when writing output:
Write tabs between fields.
Do not enclose fields within any quoting characters.
Use “\” to escape instances of tab, newline, or “\” that occur within field values.
Write newlines at the ends of lines.
Note that because LINES TERMINATED BY '\n' is the default, you could omit that clause; but the FIELDS clauses are necessary for CSV output.
Will that actually save to my local machine?
No. As documented under SELECT ... INTO Syntax:
The SELECT ... INTO OUTFILE 'file_name' form of SELECT writes the selected rows to a file. The file is created on the server host, so you must have the FILE privilege to use this syntax. file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed. The character_set_filesystem system variable controls the interpretation of the file name.
The SELECT ... INTO OUTFILE statement is intended primarily to let you very quickly dump a table to a text file on the server machine. If you want to create the resulting file on some other host than the server host, you normally cannot use SELECT ... INTO OUTFILE since there is no way to write a path to the file relative to the server host's file system.
However, if the MySQL client software is installed on the remote machine, you can instead use a client command such as mysql -e "SELECT ..." > file_name to generate the file on the client host.
It is also possible to create the resulting file on a different host other than the server host, if the location of the file on the remote host can be accessed using a network-mapped path on the server's file system. In this case, the presence of mysql (or some other MySQL client program) is not required on the target host.
I am nervous about running this script I don't want to make a mistake and mess up the database.
SELECT statements only read data from the database and do not make any changes to its content: thus they cannot "mess up the database".
Also is .txt ok for the script file?
Any extension will work: neither the MySQL client nor server software see it (your operating system reads the file and sends its content to the client program, which in turn sends that content to the server; the operating system is ambivalent to the file extension here).
On Windows, a .txt extension will associate the file with a text editor (e.g. Notepad) so that it can be readily opened for editing. Personally I would prefer .sql as it more accurately describes the file's content, and I would then associate that extension with a suitable editor—but none of that is necessary.
where is the Synthax error here?
LOAD DATA INFILE 'mysqlout_back.txt'
INTO TABLE temp (user,category,site,tld,ip,updated,date)
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n' ;
If you only want to load the data in specific columns, the go to the end:
LOAD DATA INFILE 'mysqlout_back.txt'
INTO TABLE temp FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'
(user,category,site,tld,ip,updated,date) ;
EDIT, regarding the file location in your comments:
The server uses the following rules to locate the file:
If the file name is an absolute path name, the server uses it as given.
If the file name is a relative path name with one or more leading components, the server searches for the file relative to the server's
data directory.
If a file name with no leading components is given, the server looks for the file in the database directory of the default database.
See the MySQL ref