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.
Related
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';
I am having a hard time loading my data to MySQL from a text file. I have been attempting to choose the correct delimiters but my file contains column names with each value.
The data is structured like this
{"id":"15","name":"greg","age":"32"}
{"id":"16","name":"jim","age":"42"}
the sql statement I am working on looks something like this currently
LOAD DATA LOCAL INFILE '/xxx.txt' INTO TABLE t1 FIELDS
TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n'(id, name, age);
results are being stored like this
{"id":"16", "name","greg"}
I need to do away with the column name and store the value.
any tips?
Writing a script as suggested by #Shadow will be easier to wrap around but you can check the section on json on this page how to import json-text-xml and csv data into mysql
or
Import JSON to MySQL made easy with the MySQL Shell if you are using MySQL Shell 8.0.13 (GA)
I've been asked to create an excel file saved as a .csv file to import into a mysql db. How do you format the excel file if the data goes to multiple tables in the db?
You need to create separate csv file for each table & then you can use command something like below:
LOAD DATA LOCAL INFILE './csv_data/employees.csv'
INTO TABLE employees
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(first_name,last_name,email_id );
This sample code is under presumption that csv file is comma separated , escape charater is double quote & first line contain header.
You need a CSV file for each table, or you need to import all of your data into an intermediate table and then split that out somehow using either a series of queries, a stored procedure, or a TRIGGER on the import table.
This is because the LOAD DATA INFILE system is limited to one table at a time.
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.
I have an excel file that i need to get into CSV. I export it fine but when I go to import it into a mysql db via phpMyAdmin i get a "Invalid field count in CSV input on line 1.".
Problem seems to be that the fields are not enclosed by double quotes. I just migrated to MS Excel 2007 and am not sure how to manipulate the CSV save options so that there are double quotes around the fields so my DB doesn't throw a conniption when i try to import.
Any suggestions? I'm fairly new at going from EXCEL to CSV but have gotten it to work previously.
Thanks
This worked for me after exporting from Excel as CSV and defining various options
load data infile '/tmp/tc_t.csv'
into table new_test_categories
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
ignore 1 lines
(id,category_name,type_id,home_collection,seo_tags,status_id);
I ran this at the mysql prompt.
There should be an MS-DOS format of CSV in your export drop down. Pick that one.
There should be an option in save-as advanced properties or something, but if not, you could always change the delimiter character to : or ; or | and then write a quick perl script to convert it to a quote-and-comma file.
Or you could just try a tab-separated-value file instead, I think phpMyAdmin will read TSVs as well.