
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we import CSV files into MySQL tables by using mysqlimport?
For importing CSV files into MySQL table we must have a CSV file i.e. a file having comma-separated values in it. Then we must have a MySQL table with the same name and structure. To illustrate it we are taking the following example −
Example
For example, we have Address.CSV file having the following data −
Name,LastName,Address
Mohan, Sharma, Sundernagar Saurabh, Arora, Chandigarh Rajesh, Singh, Lucknow
And we want to import these values into MySQL table named Address having the following structure −
mysql> DESCRIBE ADDRESS; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | Name | varchar(20) | YES | | NULL | | | LastName | varchar(20) | YES | | NULL | | | Address | varchar(20) | YES | | NULL | | +----------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
Now, with the help of mysql import, along with some options, we can import the values of address.csv into MySQL table named ‘address’ −
C:\mysql\bin>mysqlimport --ignore-lines=1 --fields-terminated-by=, --verbose --local -u root query C:/mysql/bin/mysql-files/address.csv Connecting to localhost Selecting database query Loading data from LOCAL file: C:/mysql/bin/mysql-files/address.csv into address query.address: Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 Disconnecting from localhost mysql> Select * from Address; +---------+----------+-------------+ | Name | LastName | Address | +---------+----------+-------------+ | Mohan | Sharma | Sundernagar | | Saurabh | Arora | Chandigarh | | Rajesh | Singh | Lucknow | +---------+----------+-------------+ 3 rows in set (0.00 sec)
The above result set shows that the values from CSV file has been imported into MySQL table.
- Related Articles
- How can we upload data into MySQL tables by using mysqlimport?
- How can we upload data into multiple MySQL tables by using mysqlimport?
- How can we import data from .CSV file into MySQL table?
- mysqlimport - A MySQL Data Import Program
- How can we import data from .txt file into MySQL table?
- How can we create a MySQL view by using data from multiple tables?
- How can we import the text file, having some line prefixes, into MySQL table?
- How can we import only specific columns from the text file, into MySQL table?
- How can we export all the data from MySQL table into a CSV file?
- How can we export some field(s) from MySQL table into a CSV file?
- How can we MySQL LOAD DATA INFILE statement with ‘ENCLOSED BY’ option to import data from text file into MySQL table?
- How can we MySQL LOAD DATA INFILE statement with ‘FIELDS TERMINATED BY’ option to import data from text file into MySQL table?
- How we can split Python class into multiple files?
- How can we set up a MySQL User account by using INSERT INTO statement?
- How can we insert data into an existing MySQL table by using PHP script?

Advertisements