How to format a CSV file for import into multiple MySQL tables - mysql

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.

Related

Is there a way to import a CSV comma delimited file into mysql?

Hi there I am new to web development.
I am trying to import a CSV file into mysql workbench using 'Table Data Import Wizard'. However, I have read my file needs to be a CSV (MS-DOS), or I get the following error: Can't analyze file. Please try to change encoding type. If that doesn't help, maybe the file is not: csv, or the file is empty.
I cannot use a CSV (MS-DOS) as my data contains a lot of different special characters including those from Nordic Europe. When I convert my CSV (comma delimited) to CSV (MS-DOS) the special characters are no longer the same.
Is there a way to import a CSV comma delimited file into mysql workbench? Or is there a better solution to getting my data into the table such as keeping the special characters the same in the MS-DOS file somehow?
You can import regular CSVs without an issue, just make sure the encoding matches.
Something like
LOAD DATA
INFILE yourfile.csv
INTO TABLE tablename
FIELDS
TERMINATED BY ','
ENCLOSED BY '"'
LINES
TERMINATED BY '\n'
IGNORE 1 LINES
should work. If your CSV doesn't have headers, remove the ignore 1 lines line from the code. If your formatting is different, change the enclosing and terminating characters accordingly.
You can look up the exact syntax in the manual.
Your CSV should work fine. You just need to Load Data Infile
You will likely need to define these settings though
LOAD DATA INFILE 'c:/tmp/discounts.csv'
INTO TABLE discounts
-- comma seperated? maybe pipes '|'?
FIELDS TERMINATED BY ','
-- what surrounds input and is it optional? then add OPTIONALLY before ENCLOSED
ENCLOSED BY '"'
-- what is at the end of files
LINES TERMINATED BY '\n'
-- how many header rows are there, if any?
IGNORE 1 ROWS;

Trouble loading data from txt file into MySQL with LOAD DATA INFILE

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)

load data infile syntax in mysql

I am using LOAD DATA local INFILE command to upload big csv file in mysql db. The field is an integer in the mysql table and so it will accept only integer.
Now if the csv file contains numbers like below then the sql doesn't work.The format of my csv file is
ID
"322"
"445"
"211"
So it is inserting 0 in the table. But if the csv file contains no double quotes then it's fine.
How can I tell "load data infile" command to ignore double quotes( as sometimes it is too difficult to remove double quotes from csv file which has got about 1 million records).My Sql is
$insert_query = "LOAD DATA local INFILE '".$target_path."'
INTO table master_huts
IGNORE 1 LINES
(hids)
";
Try adding this:
FIELDS OPTIONALLY ENCLOSED BY '"'
before:
IGNORE 1 LINES

I can import CSV file with CSV option but doesnt work for CSV with LOAD DATA

I can import CSV file with CSV option but doesnt work for CSV with LOAD DATA. I get the error
Error: 1083 - Field separator argument is not what is expected
...although I am doing everything same,uploading the same file to same table.
You are probably forgetting to set the field separator to a comma. Here's an example command for loading a CSV file from the MySQL documentation.
LOAD DATA INFILE can be used to read files obtained from external sources. For example, many programs can export data in comma-separated values (CSV) format, such that lines have fields separated by commas and enclosed within double quotation marks, with an initial line of column names. If the lines in such a file are terminated by carriage return/newline pairs, the statement shown here illustrates the field- and line-handling options you would use to load the file:
LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
Some notes:
If your first row doesn't contain headers, you should omit the IGNORE 1 LINES.
If your fields aren't enclosed by quotation marks, you can omit that too.
Lastly, if your file is
local, you should be using LOAD DATA LOCAL INFILE.

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.