Update a file using mysql - mysql

I've a database and im trying to export the data from a table to a .csv file so that i can import the data as contact data. I can create the file and write to it using this syntax:
SELECT E_Name, Email INTO OUTFILE '/xampp/tmp/result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM email WHERE 1
the problem is when i run this a 2nd time, i get an error that the file already exists. (im aware it exists as i've previously ran the query and it created it) What i would like to happen is to either, check the information is there from before and add the new information, or simpler again, overwrite the original file with the updated version.
could anyone throw some information on how to do this?
Thanks in advance,
Andrew
///////////edit\\\\\\\\\\\\\\
okay, decided to go for the timestamp method as ^^^^ cannot really be done. however im now running into an error for this aswell :$
CONCAT(SELECT E_Name, Email INTO OUTFILE '/xampp/tmp/Sample', DATE_FORMAT(now(), '%d%m%Y'), '.csv')
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM email WHERE 1
Can anyone help me with this concat problem? unexpected IDENT_QUOTED

Related

Can I LOAD DATA with ugly data that has escape characters and quotes?

I have two records for example like this
11,avec myName à EX,Ex,0,2021-06-25
22,"andone \"ttt\"",Ex,0,2021-06-25
I am trying to load this into a table with MySQL with load data, and every time I try it, the quotes get cut off, or the back spaces don't show up.
I need to know if this is even possible. Can those two records go into a table and look exactly like they are in the CSV file?
I am using MySQL and trying
LOAD DATA LOCAL INFILE 'example.csv' INTO TABLE example;
Use the ENCLOSED BY and ESCAPED BY options.
LOAD DATA LOCAL INFILE 'example.csv'
INTO TABLE example
FIELDS OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\';
LOAD DATA LOCAL INFILE 'example.csv' INTO TABLE example
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(col1, col2, col3.col4,col5);
In mysql 8 this will get you an error please read
https://dev.mysql.com/doc/refman/8.0/en/load-data.html#load-data-file-location
Windws uses LINES TERMINATED BY '\r\n'
if the file comes from a linux system use `LINES TERMINATED BY '\n'
it os hard to tell without seeing the file what parameters would help exactly so you should try some variants out`.
also a editor with hexfile capability helps in such cases to analyse the struicture

How to export a CSV file from the MySQL command line

I have a MySQL database containing a single table which is used as a temporary storage point for manipulating and querying data sets and is periodically deleted and replaced with new data. What I would like to be able to do is export the data from the MySQL command line to use for other purposes. I am using the XAMPP Apache server package with phpMyAdmin on Windows 10.
The issue I am having is the INTO OUTFILE syntax I am using returns an error relating to '\n'. Below is an example of the syntax:
SELECT *
FROM tablename
WHERE work_complete = 'Yes'
INTO OUTFILE 'C:\file path for the file to be exported to\file_name.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY "''"
ESCAPED BY '\'
LINES TERMINATED BY '\n';
I have spent some time researching this without any luck, I have even tried using
LINES TERMINATED BY '\r\n'
but the error remained the same
ERROR: Unknown command '\''.
-> LINES TERMINATED BY '\r\n';
If anyone could provide any tips that would be greatly appreciated.
Use this
SELECT *
FROM tablename
#WHERE work_complete = 'Yes'
INTO OUTFILE 'C:\file path for the file to be exported to\file_name.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ';'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
I used some other syntax as you and deleted the OPTIONALLY which mysql doesn't like at that place
The problem is not in LINES TERMINATED BY but in ESCAPED BY.
This:
ESCAPED BY '\'
Is invalid syntax, because the backslash is interpreted as an escape character for the following quote. A decent text editor should let you see that.
You need to escape the backslash, like so:
ESCAPED BY '\\'
Alternatively, you can also use '\b':
ESCAPED BY '\b'
Another probem is that OPTIONALLY ENCLOSED accepts only a single character, while you are giving it two single quotes.
In your query:
SELECT * FROM tablename WHERE work_complete = 'Yes'
INTO OUTFILE 'C:\file path for the file to be exported to\file_name.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY "'" ESCAPED BY '\\'
LINES TERMINATED BY '\n'

Error when trying export from Mysql to CSV

When I trying to export Mysql DB to CSV like SELECT * INTO OUTFILE '/tmp/test.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM reviewdb1;
I get error
ERROR 1046 (3D000): No database selected
However I sign reviewdb1 properly.
So I switch to reviewdb1
mysql> use reviewdb1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SELECT * INTO OUTFILE '/tmp/test.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM reviewdb1;
ERROR 1146 (42S02): Table 'reviewdb1.reviewdb1' doesn't exist
and it seems like something wrong in my query (syntax)
Could you give me advice, what exactly ?
Thanks in advance.
Update:
it's dawn on me I should sign TABLE in the DATABASE which I want to export to .csv.
SELECT * FROM account_diff_preferences INTO OUTFILE '/var/lib/mysql-files/test.csv' FIELDS TERMINATED BY ',';
this command works fine, but may I export whole database like this or only one table from database per command accepted ?
now I realize it was pretty stupid question.
excuse me.

Using this code to export mysql table to csv - but I need syntax for MAC not PC

Code for windows environment is below:
SELECT *
INTO OUTFILE '/documents/products.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM products
Just need the 2nd line for MAC environment - thanks.
First of all you need to put your FROM clause before INTO OUTFILE.
Second, be sure that the directory specified in OUTFILE has the ability for MySQL to write to it (in your case /documents). You'll have a much easier time writing to /tmp (MySQL already has permissions to write by default) than trying to change permissions on another directory to accept writes form your MySQL database.
SELECT *
FROM products
INTO OUTFILE '/tmp/products.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n';

How do I export csv file to my computer in mysql

I am trying to export a table from a remote server to my desktop computer in csv format. I have this code:
select * from order
into outfile 'C:\Users\Sleep Shop\Desktop\MySQL Scripts/outfile.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n';
but I get this error:
failed : Can't create/write to file '/var/lib/mysql/C:\Users\Sleep Shop\Desktop\MySQL Scripts/outfile.csv' (Errcode: 2)
I'm thinking there is something fundamental I don't understand about this procedure, probably something to do the table being at a remote server. Can anyone help?
I used this code to tell a spot on the server to create the file:
select * from orders
into outfile '/var/www/test/outfile.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n';
It creates the file but it contains no records and I get this error:
failed : Field separator argument is not what is expected;
Change the query like this:
select * from `order`
into outfile 'export.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n';
Then you will find the file in your remote server's directory here: /var/lib/mysql/export.csv (or possibly /var/lib/mysql/data/your-db-name/export.csv)
Connect to your server via SSH (use putty) and transfer the file to your PC or move the file to a directory that accepts FTP access and you can download it using an FTP client (ie. filezilla, winSCP).
Or you can use phpMyAdmin and click on the table, then click the "export" tab, and then you will see an option to select "CSV" from the format dropdown. This may not work if your table is too large (depends on phpMyAdmin's settings or PHP's settings on how long a script can run).