Export MySQL Table to CSV file through Batch File - mysql

I need to export my MySQL tables to CSV files with a batch file. Is there any option like that?
I am getting many ETL references in Google search, but is there any command line arguments, which can be used in batch file so that I can export the data in CSV format with headers?
There is an URL export mysql table to csv file using batch script regarding this, but not clear, what to write in the batch file.
Can anyone please help me?

SELECT * FROM firstdb.user
INTO OUTFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/employee_backup.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ';'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';

Related

Importing CSV file to HeidiSQL, error 1148

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;

Export database record as CSV file

Is there any way to export database records as CSV file from Mysql Workbench by using command line syntax?
SELECT orderNumber, status, orderDate, requiredDate, comments
FROM orders
WHERE status = 'Cancelled'
INTO OUTFILE 'C:/tmp/cancelled_orders.csv'
FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
more information here:
http://www.mysqltutorial.org/mysql-export-table-to-csv/
MySQL Workbench comes with a table data export function, which supports csv and json formats. You can start it from the context menu in the schema tree:
The simple wizard then allows you to select the output data type:
Similarly, there's the table data import wizard that can read csv and json data.

How to import xls file in to MySQL table

I'm using ubuntu 14.04 operating system and I want to import excel sheet(.xls) in to database.
I am using phpmyadmin mysql administration tool. If any one can give me the commands to import xls to mysql I would be grateful.
Import CSV file in to MySQL Table
Change file type in to CSV
Create table according to excel column names
Place the csv file in /var/lib/mysql
Open phpmyadmin sql command pane:
Execute this sql command
LOAD DATA INFILE 'testlist.csv'
INTO TABLE TEST
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Use the load data capability. See
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Export CSV file in to MySQL Table
SELECT ... FROM ... WHERE ...
INTO OUTFILE 'testlist.csv'
FIELDS TERMINATED BY ','
Here is an example that produces a file in the comma-separated values (CSV) format used by many programs:
SELECT * INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;

How can I import data from excel to MYSQL?

I tried to import the data from excel to MYSQL by using the following command:
load data local infile "filepath" into table mytab;
But this command is work for import the data from text file not for csv file or xlsx. I want to import the data directly from excel to MYSQL.
check following,
see the seperator in your csv file and make sure you provide the correct one in command.
Referance Link
You need to convert your Excel sheet into a CSV file, or use MySQL for Excel to help with the process.
load data local infile "filepath" into table mytab fields terminated by ';' enclosed by '"' lines terminated by '\r\n';
Should be the command you need, but you should export to csv first.

Export a SQL database into a CSV file and use it with WEKA

How can I export a query result from a .sql database into a .csv file? I tried with
SELECT *
FROM players
INTO OUTFILE 'players.csv'
FIELDS TERMINATED BY ','
LINES TERMINATED BY ';';`
and my .csv file is something like:
p1,1,2,3
p2,1,4,5
But they are not in saparated columns, all are in 1 column.
I tried to create a .csv file by myself just to try WEKA, something like:
p1 1 2 3
p2 1 4 5
But WEKA recognizes p1 1 2 3 as a single attribute.
So: how can I export correctly a table from a sql db to a csv file? And how can I use it with WEKA?
EDIT
Is it possible to add a header line with columns names? I have read something online but I am not able.
I'm using MySQL 5.5.
You need to inform weka about format of your csv file. You can do so while opening csv file and invoking options dialog, see below images. Also you need to terminate your lines with "\n" instead of ";".
Try this.
First of all convert the .sql file to .csv format. For eg:
SELECT orderNumber, status, orderDate, requiredDate, comments
FROM orders
INTO OUTFILE 'C:/tmp/cancelled_orders.csv'
FIELDS ENCLOSED BY '"' TERMINATED BY ',' ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
NOTE: Weka tool need the .csv file`s attributes terminated by a comma. If you use semi colon, it wont work. You will get an exception.
Now this will convert the .sql format to .csv format which you can find in the C:/tmp/cancelled_orders.csv location.
Finally start the weka tool and fetch this data set to it.
NOTE : Make the "Files of Type" as "CSV data files".
Then it will work without giving any exception. It works.