Sql to text file - mysql

I have a SQL database and want to write a script, which saves the table "European_option_info" as a txt (or excel) file to a local folder.
I use the following code:
use database;
select * into outfile 'C:\[...]\Dropbox\Employees.txt' FIELDS TERMINATED BY ';' from European_option_info;
This gives me the following error:
"Error Code: 1. Can't create/write to file 'C:[]\Employees.txt' (Errcode: 22)"
Regards,
Daniel

It looks like your file name is incorrect. Use a full path for the path to Employees.txt.
For example
USE database;
SELECT * INTO OUTFILE 'C:\Users\Username\Dropbox\Employees.txt'
FIELDS TERMINATED BY ';'
FROM European_option_info;
Or whatever the full path to your Dropbox folder is.

One problem may be of the file path that your are specifying, you need to to specify entire path and also that file must not be open when executing this query.
The other problem may be of permission to create/change something in C:\\( Here I am assuming that you are using Windows OS and if not then just try this solution for other OS as well) as it may require administrative rights as the application that is executing your query may not be in run as administrator mode of windows. (Assuming C: drive contains your windows installation).
So try changing the directory to some other directory of your PC.

Related

Using select info outfile

I am trying to send the results of a query to a file using
SELECT * from TABLENAME INTO OUTFILE 'spool.txt' ;
but I can't find the resulting file
The command executes, but I don't know where to find the file.
I did some research, tried to use a full path but there are permission problems.
I did
SHOW VARIABLES LIKE "secure_file_priv";
to see the directory where I thought the output would go, it said
C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\
But when I look in that directory the file is not there. What am I missing? Thanks!

File not found in MySQL data directory

I am trying to load an XML file into a table on my localhost MySQL server. Per MySQL 5.6 refman page I have my file loaded in the Data directory however I keep getting the error that my file is not found. I executed the SHOW VARIABLES WHERE Variable_Name LIKE "%dir" command and found where my data dictionary is located (C:\ProgramData\MySQL\MySQL Server 5.6\Data) and it's where I put my xml file but still get the same error:
mysql> USE test
Database changed
mysql> LOAD XML LOCAL INFILE 'testXML.xml'
-> INTO TABLE testxml
-> ROWS IDENTIFIED BY '<Data>';
ERROR 2 (HY000): File 'testXML.xml' not found (Errcode: 2 - No such file
or directory)
Any suggestions/direction would be appreciated. Thanks.
I remember having such a problem once and the problem was the spaces in the path name.
If really the file is at the right place and a file not found occur, I would highly suspect that the folder name 'MySQL Server 5.6' is the problem. You can validate this easily, put the file in another folder instead of the data dir and try adding/removing spaces to the folder name and see if it works.
An other possibility is that your folder is located in an Admin directory and that you don't have right to access.

why does SELECT INTO OUTFILE give file exists error even though file does not exist?

The file definitely does not exist, but I am getting an error anyway.
I do:
$ rm /tmp/records_materialized_view.txt;
$ mysql ...
> SELECT * FROM records_materialized_view INTO OUTFILE '/tmp/records_materialized_view.txt';
ERROR 1086 (HY000): File '/tmp/records_materialized_view.txt' already exists
SELECT INTO OUTFILE writes results to a server file.
Are you checking for the file existence on server?
If you want to select into a local file on your client machine, just redirect mysql output:
mysql mydb < script.sql > /tmp/records_materialized_view.txt
Came across this answer when I had a similar issue.
I realized that SELECT INTO OUTFILE does not overwrite files, you have to clean them up yourself. So you will get this error the next time you write it. Not sure what is meant by in the above answer, that the files are written to a server file.
Also, SELECT INTO OUTFILE by default writes files relative to your db data directory. The default location i.e.
/var/lib/mysql/
So if you check your db data directory, you should find your files there. Use an absolute path to control exactly where you want the file to end up. This is exactly what I was after, hopefully it helps somebody. Cheers.

mysql: SOURCE error 2?

When I tried to source for a particular .sql file, namely 'metropolises.sql' that I created and saved previously from a database, the following error is displayed:
Failed to open file 'metropolises.sql', error: 2
Any idea what went wrong?
Assuming you mean that you are trying to use the source command in order to execute SQL statements from a text file, the error number given appears to be passed through from the POSIX layer.
Therefore, using this resource, we can deduce that the error value of 2 means "no such file or directory".
In short, you got the path wrong.
Try providing an absolute path, as it's not clear what the current working directory will be in the context of your MySQL server. You may be assuming that it's the working directory of your shell, but it's not obvious that we should expect this to be true.
Just use the absolute path of the file and then, instead of using backslashes, use forward slashes.
Example:
with backslashes : source C:\folder1\metropolises.sql
with forward slashes : source C:/folder1/metropolises.sql
IF YOU ARE USING MYSQL INSIDE DOCKER
Note that if you are running MySQL inside docker then you must first copy the dump into your MySQL docker environment. To do that follow the steps below
First, check and copy the container ID for your MySQL docker by:
sudo docker ps
Copy the SQL dump file into your container using:
sudo docker cp /path/to/sql/file.sql MysqlDockerID:/
This will copy the dump file into the docker root folder if you want to copy the file inside any other directory/path inside docker replace the '/' after 'MysqlDockerID:' with the path you want appropriate one.
Now to interact with MySQL inside a running container run the following command:
sudo docker exec -it MysqlDockerID bin/bash
Now connect to the MySQL using the terminal by:
mysql -u yourUserName -p
This will now ask you for the password. Enter the correct password to proceed.
List the databases available by:
show Databases;
This will list out the available databases
Assuming your database name where you want to import dump to is 'MyDatabase'. Switch to that using:
use MyDatabase
Now you can import the file by typing:
source file.sql
Remember the above command works if you have copied your file into your root folder (using step 2). If you have copied it to any other path make sure you use that same path instead
Related issue I had getting error 2 running source command: filename must not be in quotes even if it contains spaces in name or path to file.
It's probably the file path to your file. If you don't know the exact location of the file you want to use, try to find your file in Finder, then drag the file into Terminal window
mysql> SOURCE dragfilePathHere
I first reach to the file
c:\windows>cd c:\akura\Db Scripts
c:\akura\Db Scripts>mysql -u root -p root
mysql>\. EXECUTER_NEW_USER.sql
here EXECUTER_NEW_USER.sql my file name
I've had the same error on Windows.
I solved it with (after on cmd: mysql -u root):
mysql> SOURCE C:/users/xxx/xxxx/metropolises.sql;
Be sure you type the right file path
On my windows 8.1, and mysql 5.7.9 MySQL Community Server (GPL),
I had to remove the ; after the file path.
This failed: source E:/jokoni/db/Banking/createTables.sql;
This Worked: source E:/jokoni/db/Banking/createTables.sql
(without termination, and forward slashes instead of windows' backslashes in path)
If you are using vagrant ensure that the file is on the server and then use the path to the file. e.g if the file is stored in the public folder you will have
sql> source /var/www/public/xxx.sql
Where xxx is the name of the file
I got this error in mysql command line using this query:
source `db.sql`;
I changed the above to the following to make it work:
source db.sql;
On my Mac, this is the only solution worked for me.
https://stackoverflow.com/a/45530305/5414448
1 - Download the .sql file and remember it's location.
2 - Open your mysql from command prompt or terminal.
3 - Create a database with the same name as that of the database present in the .sql file (create database your_database_name)
4 - Now exit out from the mysql command line client
5 - Now try and execute this command =>
mysql -u your_username -p your_database_name < your_sql_file_with_complete_location
example - mysql -u root -p trial < /home/abc/Desktop/trial.sql
here my .sql file is named trial and is present in the desktop, the database is also name trial
6 - You should now have your sql file imported to the corresponding mysql database.
I was having this issue and it turns out if you are using wamp server to run mysql, you have to use the file path within the wamp64 folder. So when the absolute path is: C:/wamp64/www/foldername/filename.sql
The path you have to use is:
www/foldername/filename.sql
May be the file name or path you are used may be incorrect
In my system i created file abcd.sql at c:\
and used command mysql> source c:\abcd.sql Then i got result
If you're on Debian 8 (Jessie) Linux, try to cd into the directory of the 'metropolises.sql'. Run mysql and execute SOURCE ./metropolises.sql;
Basically, try the relative path. I tried this and it works.
If you are running dockerized MySQL container such as ones from this official Docker Image registry: https://hub.docker.com/_/mysql/ You may encounter this issue as well.
For me it was because the file was located on a shared drive and it could not access, for some reason, to that path.
I cut the file and put it on my local drive in a path without spaces and it got resolved.
I got the same error when i used the command source and gave the sql file path by drag n dropping it.
Then I just had to remove those single quotes which appeared by default with drag and drop, a space before file extension and it worked.
soln:
source /home/xyz/file .sql ;(path and a space before file extension)
For Mac users, you can give the path as,
source /Users/YOUR_USER_NAME/Desktop/metropolises.sql;
*I have assumed that the file you need to source is in your desktop and the file name is metropolises.sql
If you type,
source metropolises.sql
then the file will be look in the default location,
/Users/YOUR_USER_NAME/metropolises.sql;
The solution for me was file permissions in Windows. Just give full control in the file to all users and it will work. After the import, get the permissions back to what it was before.
I had a problem on my Mac because one of the folder (directory) names in the path had a space in the name. BTW, the space was escaped by a \, but that was not understood by mySQL
solution - 1) Make sure you're in the root folder of your app. eg app/db/schema.sql.
solution - 2) open/reveal the folder on your window and drag&&drop in the command line next to keywork source (space) filesource. eg source User/myMAC/app/db/schema.sql
I get into this problem in my Xubuntu desktop. I fixed it by renaming all my files and folders so there is no space in the file path.
Passing the full path does not error, but if the folders have MySQL spaces it does not recognize the .sql file.
I have MySQL 8.0.21 on OS UNIX.
I've got the same error on Windows. I solved it running the code on MySQL command line
source c:\Users\xx\Downloads\data_file.sql
Remove spaces in the folder names of the path, It worked for my mac path.
(Eg: change the folder name MySQL Server 5.1 to MySQLServer5.1)
I also got the same message when I try from the MySQL console. However, when I open the command prompt and do the same steps it works.
C:\Users\SubhenduD>cd ../
C:\Users>cd ../
C:\>cd \xampp\mysql\bin
C:\xampp\mysql\bin>mysql -u -root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 68
Server version: 5.6.16 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use balticktravels;
mysql> source balticktravels.sql;

mysql load data infile can't get stat of file Errcode: 2

I have looked all over and found no solution, any help on this would be great.
Query:
LOAD DATA INFILE '/Users/name/Desktop/loadIntoDb/loadIntoDB.csv'
INTO TABLE `tba`.`tbl_name`
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(
field1, field2, field3
)
Error:
Can't get stat of '/Users/name/Desktop/loadIntoDb/loadIntoDB.csv' (Errcode:2)
NOTE:
I'm running MySQL Query browser on OSX 10.6.4 connecting to MySQL 5.x
Things I've tried:
Drag-n-drop
Chmod 777
Put in a folder with 777 permissions
as well as the file having 777
permissions
try to use LOAD DATA LOCAL INFILE instead of LOAD DATA INFILE
otherwise check if apparmor is active for your directory
I had a similar problem. The resolution was a mildly ugly hack, but much easier to remember than apparmor workarounds provided that you can 'sudo'. First, I had to put the input file in the mysql sub-directory for the database I was using:
sudo cp myfile.txt /var/lib/mysql/mydatabasename
This does a copy and leaves 'root' as the file owner. After getting into mysql and doing a USE mydatabasename, I was able to populate appropriate table using
LOAD DATA INFILE 'mytabdelimitedtextfile.txt' INTO TABLE mytablename;
Using --local parameter will help with this.
Example: mysqlimport --local databasename file.txt -p
source:
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
"The --local option causes mysqlimport to read data files from the client host"
For me, copying the contents to /tmp and using that as the source folder did the trick.
I use MariaDB, and my version does not allow using the "LOCAL" modifier.
Interestingly, giving read-write access to the CSV folder did not work either.
I had the same problem while populating a table in mysql on a AWS instance.
In my case i had the csv file in the instance itself.
Putting the Absolute path solved my problem.
Here's the line from MySQL documentation
If LOCAL is specified, the file is read by the client program on the client host and sent to the server. The file can be given as a full path name to specify its exact location. If given as a relative path name, the name is interpreted relative to the directory in which the client program was started.
http://dev.mysql.com/doc/refman/5.7/en/load-data.html