outfile not in expected location despite specifying path - mysql

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.

Related

Unable to use LOAD DATA INFILE | -secure-file-priv issue

I have tried many different possible solutions to resolve this issue but none seem to be fixing it for me.
I've tried solutions from other articles on here but none seem to be working
I am running MySQL 8.0 (LOAD DATA LOCAL INFILE is not supported in this version)
Im trying to load data from a csv file into my tables by means of LOAD DATA INFILE. When I run the script, I receive this error:
Error Code: 1290. The MySQL server is running with the
--secure-file-priv option so it cannot execute this statement
My code:
LOAD DATA INFILE 'customer.csv'
INTO TABLE customer
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
I have tried the following
In the '' put the full directory of the file in different formats, including trying both / and \
Remove the Secure-File-Priv from the config file
Changed the directory to the folder on my Desktop
Left the directory empty (Secure-File-Priv = "" {Also tried removing the ""})
I checked the directory by running the query and it says it is where I have it set to on my Desktop
Is there anything else that I could try to load data into my tables or to get the Secure File Priv block removed?
Update
This is the code with the error when the secure-file-priv is left empty:
LOAD DATA INFILE 'C:/users/user/desktop/college/advanced db/uploads/customer.csv'
INTO TABLE customer
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Error Code: 29. File 'C:\users\carl\desktop\college\advanced
db\uploads\customer.csv' not found (OS errno 13 - Permission denied)
Assuming that you are using Windows 7 or more:
Search "services" in the windows research table (left-down of the screen)
Search the Mysql service in the table and shut down it
Open the my.ini file (i assume "C:\ProgramData\MySQL\my.ini")
Search for Secure-File-Priv and assign "".
Restart the process via "services" or Mysql workbench
Put the .csv file into the directory of the database where exists the table (in this case "customer"), this is the possible path: C:\ProgramData\MySQL\MySQL Server 8.0\Data\.
Execute the query

Not able to load data from .csv file to mysql database using shell script

I am not able to load data from .csv file to MySQL database using shell script, I am using this command in Linux to import data:
mysql -utest -ptest123 --local_infile -e "use testing; LOAD DATA LOCAL INFILE '20160821-105501.cap.csv' INTO TABLE DataPackets FIELDS TERMINATED BY ',' ENCLOSED BY '"' IGNORE 1 LINES;"
But I always get stuck getting this sign ">"
I want to use the above command in shell script to automate the process.
You can use in shell as follow, I haven't tried but it may work for you..
mysql -utest -ptest123 --local_infile << END
use testing; LOAD DATA LOCAL INFILE '20160821-105501.cap.csv' INTO TABLE DataPackets FIELDS TERMINATED BY ',' ENCLOSED BY '"' IGNORE 1 LINES;
END
Hope it helps!

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.

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

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.

.db file and MySQL

I am having real issues with a .db file its around 20gb in size with three tables and the rest data.
I am on a mac so i am having to use some crappy apps but it wont open in Access.
Does any one know what software will produce a .db file and what software will allow me to open it and export it as a CSV or MySQL file ?
Also if the connection was interrupted during transit could this effect the file ?
Since mac is BSD-based now, try opening a terminal and executing the command file /path/to/large/db -- it should tell you at least what file type the DB is, and from there you can determine what program to use to open it. It might be MySQL, might be PostGreSQL, might be SQLite -- file will tell you.
Example:
$ file a.db
a.db: SQLite 3.x database
$ file ~/.kde/share/apps/amarok/mysqle/amarok/tracks.{frm,MYD,MYI}
~/.kde/share/apps/amarok/mysqle/amarok/tracks.frm: MySQL table definition file Version 10
~/.kde/share/apps/amarok/mysqle/amarok/tracks.MYD: data
~/.kde/share/apps/amarok/mysqle/amarok/tracks.MYI: MySQL MISAM compressed data file Version 1
So it's SQLite v3? Then try
sqlite3 /path/to/db
and you can perform pretty much standard SQL from the CLI. At the CLI, you can type .tables to list all the tables in that DB. -- Or if you prefer a GUI, there are a few options listed in this question. Accepted answer was SQLite manager for Firefox.
Then you could drop tables or delete as you see fit.
Here's an example of dumping a csv to stdout:
$ sqlite3 -separator ',' -list a.db "SELECT * FROM t"
3,4
3,5
100,200
And to store it to a file -- the > operator redirects output to a file you name:
$ sqlite3 -separator ',' -list a.db "SELECT * FROM t" > a.csv
$ cat a.csv # puts the contents of a.csv on stdout
3,4
3,5
100,200
-separator ',' indicates that fields should be delimited by a comma; -list means to put row data on the same line, using the delimiter; a.db indicates which db to use; and "SELECT * FROM t" is just the SQL command to execute.
I'm not a Mac user but if it's a SQLite file I've heard great things about Base.