What is the best/fastest way to upload a csv file into a mysql table? I would like for the first row of data be used as the column names.
Found this:
How to import CSV file to MySQL table
But the only answer was to use a GUI and not shell?
What is the best/fastest way to upload a csv file into a mysql table? I would like for the first row of data be used as the column names.
Found this:
How to import CSV file to MySQL table
But the only answer was to use a GUI and not shell?
To load data from text file or csv file the command is
In above command, in my case there is only one column to be loaded so there is no "terminated by" and "enclosed by" so I kept it empty else programmer can enter the separating character . for e.g . ,(comma) or " or ; or any thing.
**for people who are using mysql version 5 and above **
Before loading the file into mysql must ensure that below tow line are added in side
etc/mysql/my.cnf
to edit my.cnf command is
sudo vi /etc/mysql/my.cnf
As others have mentioned, the load data local infile works just fine. I tried the php script that Hawkee posted, but didnt work for me. Rather than debug it, here's what i did:
1) copy/paste the header row of the CSV file into a txt file and edit with emacs. add a comma and CR between each field to get each on on it's own line.
2) Save that file as FieldList.txt
3) edit the file to include defns for each field (most were varchar, but quite a few were int(x). Add create table tablename ( to the beginning of the file and ) to the end of the file. Save it as CreateTable.sql
4) start mysql client with input from the Createtable.sql file to create the table
5) start mysql client, copy/paste in most of the 'LOAD DATA INFILE' command subsituting my table name and csv file name. Paste in the FieldList.txt file. Be sure to include the 'IGNORE 1 LINES' before pasting in the field list
Sounds like a lot of work, but easy with emacs.....
Instead of writing a script to pull in information from a CSV file, you can link MYSQL directly to it and upload the information using the following SQL syntax.
To import an Excel file into MySQL, first export it as a CSV file. Remove the CSV headers from the generated CSV file along with empty data that Excel may have put at the end of the CSV file.
You can then import it into a MySQL table by running:
as read on: Import CSV file directly into MySQL
EDIT
For your case, you'll need to write an interpreter first, for finding the first row, and assigning them as column names.
EDIT-2
From MySQL docs on
LOAD DATA
syntax:Therefore, you can use the following statement:
I wrestled with this for some time. The problem lies not in how to load the data, but how to construct the table to hold it. You must generate a DDL statement to build the table before importing the data.
Particularly difficult if the table has a large number of columns.
Here's a python script that (almost) does the job:
The problem it leaves to solve is that the final field name and data type declaration is terminated with a comma, and the mySQL parser won't tolerate that.
Of course it also has the problem that it uses the TEXT data type for every field. If the table has several hundred columns, then VARCHAR(64) will make the table too large.
This also seems to break at the maximum column count for mySQL. That's when it's time to move to Hive or HBase if you are able.
Here's a simple PHP command line script that will do what you need:
It will create a table based on the first row and import the remaining rows into it. Here is the command line syntax:
Import CSV Files into mysql table
visits : http://www.webslessons.com/2014/02/import-csv-files-using-php-and-mysql.html