Can't create/write to file (Errcode: 22) - mysql

Quite new with SQL I'm looking to export some data from a MySQL database into a csv file. I'm working locally (localhost).
Here is my SQL statement:
SELECT DISTINCT *
INTO
OUTFILE 'C:\Users\Martin\Downloads\result.csv'
FROM provider, location, provider_has_location
WHERE
provider.idprovider = provider_has_location.provider_idprovider AND
location.idLocation = provider_has_location.location_idLocation
LIMIT 20
MySQL return the following error:
Can't create/write to file 'C:UsersMartinDownloads esult.csv' (Errcode: 22)
Thanks for your help.

It looks like the back-slashes may be affecting the command. Try using '\\' instead of '\'.

SELECT * FROM TableName WHERE condition INTO OUTFILE 'E:/test/filename.csv';
You are having issue with the back slash. Using forward slash resolved the issue in windows machine.
But in Ubuntu its not working.
Then I tried with the below mentioned Query and it works:
SELECT * FROM TableName WHERE condition INTO OUTFILE '//tmp/test/filename.csv';
Here the double slash when starting the path is necessary.

Related

Windows and Linux file path issues using python SQL load data infile

I am working on a mysql (8) db which is too big for the 2TB linux partition size so I have moved the mysql instance onto a 16TB nvme raid under windows 10. All my other code is running on Debian 10 on a virtualbox instance and I have mapped a perm drive between the Debian VM and the nvme raid array.
I can open the database from debian and read and write as normal, so the odbc connector is working fine.
The issue here is the fact I am loading very large json log files into one table and doing it a row at a time was taking hours, so I opted to create a csv file for each log file and use LOAD DATA INFILE as part of the SQL statement.
Trouble is, when I execute the sql statement I get the 'file not found' issue, even though, looking at debug code, the file path is correct and it actually exists.
An excert from my python 3 code is:
p = f"/media/sf_unpack/{filename}"
try:
with open(p, 'r', buffering=1024 * 1024) as csvfile:
print(csvfile.read())
SQL = f"LOAD DATA INFILE '{p}' INTO TABLE xxx fields terminated by ',' LINES TERMINATED BY '\n' (field,field,.....etc) ;"
try:
mycursor.execute(SQL)
connection_object.commit()
except Exception as ex:
displayerror(ex)
This code will open the file correctly and show a value for p of /media/sf_unpack/filename.csv (which is correct).
When we get to the mycusror.execute(SQL) is raises an exception and says the directory or filename cannot be found. Interestingly, and I am sure this is the issue, the dubugger tells me the file that cannot be found is a windows version D:\media/sf_unpack/filename.csv - which looks as if it has something to do with the virtualbox mapping.
i.e. p =
I have tried to use the Path method from pathlib i.e p = Path(f"D:\mysql\unpack\{filename}" but that makes no difference.
I know I am doing something stupid but I am not sure what it is.
Any help would be gratefully recieved

DatabaseError: 1 (HY000): Can't create/write to file '2015-04-06 20:48:33.418000'.csv (Errcode: 13 - Permission denied)

I am designing an application in Python and trying to write to a CSV file, but I am getting this error:
DatabaseError: 1 (HY000): Can't create/write to file '2015-04-06 20:48:33.418000'.csv (Errcode: 13 - Permission denied)
The Code:
def generate_report(self):
conn=mysql.connector.connect(user='root',password='',host='localhost',database='mydatabase')
exe2 = conn.cursor()
exe2.execute("""SELECT tbl_site.Site_name, State_Code, Country_Code,Street_Address, instrum_start_date, instrum_end_date, Comment INTO OUTFILE %s FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n'FROM tbl_site JOIN tbl_site_monit_invent ON site_id = tbl_Site_site_id """, (str(datetime.datetime.now()),))
I can run this code without any errors on a Mac, but I need it to work on Windows.
How can I resolve this error?
Simple really. A colon character is not a valid character in a filename on Windows. It's not allowed.
Reference: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
The colon character is in the list of "reserved characters", along with several others. (NOTE: One use of the colon character is as a separator for an Alternate Data Stream on NTFS. Ref: http://blogs.technet.com/b/askcore/archive/2013/03/24/alternate-data-streams-in-ntfs.aspx
Followup
The question has been significantly edited since my previous answer was provided. Some notes:
I'm not very familiar with running MySQL on Windows OS. Most of my work with MySQL server is on Linux.
The SELECT ... INTO OUTFILE statement will cause the MySQL server to attempt to write a file on the server host.
The MySQL user (the user logged in to MySQL) must have the FILE privilege in order to use the SELECT ... INTO OUTFILE statement.
Also, the OS account that is running MySQL server must have OS permissions to write a file to the specified directory, and the file to be written must not already exist. Also, the filename must conform to the naming rules for filenames on OS filesystem.
Ref: https://dev.mysql.com/doc/refman/5.5/en/select-into.html
For debugging this type of issue, I strongly recommend you echo out the actual SQL text that is going to be sent to the MySQL server. And then take that SQL text and run it from a different client, like the mysql command line client.
For debugging a privileges issues, you can use a much simpler statement. Test writing a file to a directory that is known to exist, that is known the mysql server has permissions to write files to, and with a filename that does not exist and that conforms to the rules for the OS and filesystem.
For example, on a normal Linux box, we could test with something like this:
mysql> SELECT 'bar' AS foo INTO OUTFILE '/tmp/mysql_foo.csv'
Before we run that, we can easily verify that the /tmp directory exists, that it is writable by the OS account that is running the mysql server, and that the filename conforms to the rules for the filesystem, and that the filename doesn't exist, e.g.
$ su - mysql
$ ls -l /tmp/mysql_foo.csv
$ echo "foo" >/tmp/mysql_foo.csv
$ cat /tmp/mysql_foo.csv
$ rm /tmp/mysql_foo.csv
$ ls -l /tmp/mysql_foo.csv
Once we get over that hurdle, we can move on to testing writing a file to a different directory, a file with a more more complex filename. Once we get that plumbing working, we can work on getting actual data, into a usable csv format.
The original question seems to indicate that the MySQL server is running on Windows OS, and it seems to indicate that the filename attempting to be written contains semicolon characters. Windows does not allow semicolon as part a filename.
It was simply permission error.

outfile not in expected location despite specifying path

I ran the following command in the MySQL command line client, but I cannot find the outfile:
SELECT my_fieldname INTO OUTFILE 'D:\mypath\my_filename.txt'
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\\n'
FROM my_tablename;
The command line prints out the following confirmation that the command ran properly:
Query OK, 9889 rows affected (0.02 sec)
But the file is not visible in D:\mypath\my_filename.txt and a search for my_filename.txt in the searchbox in the windows start menu does not produce any results.
This is being run on an instance of MySQL on my local development machine running Windows 7.
Where can I find the outfile? Or how can I change my command so that the outfile is actually created?
You need to escape backslashes in strings:
SELECT my_fieldname INTO OUTFILE 'D:\\mypath\\my_filename.txt'
or switch to forward slashes:
SELECT my_fieldname INTO OUTFILE 'D:/mypath/my_filename.txt'
Also, make sure that the MySQL server process is allowed to write into D:\mypath.

specify OUTFILE path

I am using MySQL database on a Ubuntu machine, and running the following SQL statement:
SELECT id,name FROM cars
INTO OUTFILE 'my_cars.dat'
LOAD DATA INFILE 'my_cars.dat' INTO TABLE all_cars(id, name);
It works. But how can I also specify the path to where the my_cars.dat file is put??
I mean , for example, I want the my_cars.dat file to be put under /data/temp , how can I specify this??
I tried to use ... INTO OUTFILE '/data/temp/my_cars.dat' ... but it does not work...
I got error :
ERROR 1 (HY000): Can't create/write to file '/data/temp/my_cars.dat' (Errcode: 13)
What user are you running mysql as? Does it have write permissions to /data/temp?
A couple potential reasons:
(and I suspect it's this) The outfile is written by the client, so it can only write to where you have write privileges. Assuming /data/temp exists (see #2), it is most likely owned by mysql:mysql or root:root, and privs 700.
MySQL's default location (in Ubuntu, at least) is /var/lib/mysql, so make sure that /data/temp exists.

MySQL cannot find data file for a load operation

I would like to load a data file into MySQL using the following command:
LOAD DATA LOCAL INFILE '/Users/David/Desktop/popularity20110511/test_data' INTO TABLE test_table
The above command gives me the following error:
#7890 - Can't find file '/Users/David/Desktop/popularity20110511/test_data'.
I've also tried:
LOAD DATA INFILE '/Users/David/Desktop/popularity20110511/test_data' INTO TABLE test_table
I also gives me an error:
#13 - Can't get stat of '/Users/David/Desktop/popularity20110511/test_data' (Errcode: 13)
I've repeatedly checked the file path and name and I've also made sure the file privilege is set to Read & Write for everyone.
I am using a Mac and phpMyAdmin.
Any suggestions on what the problem may be?
I had the same problem using MacOs and tried to change permissions, etc, but I realized you have to use the same directory structure you have using in the Terminal Application. Example: if you have (localhost/myproject/myfile.csv) try using
(Applications/XAMPP/htdocs/myproject/myfile.csv).
LOAD DATA LOCAL INFILE '/Applications/XAMPP/htdocs/myproject/myfile.csv'
INTO TABLE `mytable`
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\r';
I'm not too sure what the problem is but I made it work by moving the file to /tmp/test_data and used LOAD DATA INFILE...
I have had the same issue, trying to import an SQL file that uses LOAD DATA LOCAL INFILE... to import a CSV file in phpMyAdmin and got the same error message:
#7890 - Can't find file 'myfile.csv'
The solution I found was to put the file in the same folder as phpMyAdmin.
I was having the same problem
'C:/Program Files/DatabaseTableHolders/Menu.csv'
7890 - Can't find file '
The first thing I did was move the files to the "Program File" directory
It still wouldn't work
Then I changed the path address from
'C:/Program Files/DatabaseTableHolders/Menu.csv'
to
'C:\Program Files\DatabaseTableHolders\Menu.csv'
THIS WORKS!!!
For me its something to do with the path structure.
By the way I'm using Eclipse and phpMyAdmin on WAMP (windows operating system). I hope this helps.
Yes, I meet the same error.
My situation:
XAMPP + MAC OS 10.9
load data local infile '/Applications/XAMPP/xamppfiles/htdocs/jsonSQL.txt' into table `ttlegs` fields terminated by ',' lines terminated by '\n'
and this works when I put jsonSQL.txt to htdocs.
It is best if you put that text file in 'xammp /phpMyAdmin ' directory ( I assume you work on xammp) That's it. then LOAD DATA LOCAL INFILE will work. Happy Coding