MySql import CSV file or tab-delimited file

MySql import CSV file or tab-delimited file

Sometimes we need to import some data from another library into another library, and this data is stored in a CSV file. For MySQL database, we have two ways: one is to use commands to import, and the other is that many MySQL clients (such as Heidisql, Sequel pro) provide us with such functions. Below we will introduce two import methods respectively.

MySql command import

Preparation:

First create a table:

The table creation statement is as follows:

CREATE TABLE `city_china` (
 `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key',
 `ename` VARCHAR(12) NULL DEFAULT NULL COMMENT 'Abbreviation of the province',
 `ecityname` VARCHAR(12) NULL DEFAULT NULL COMMENT 'City name',
 `ccityname` VARCHAR(12) NULL DEFAULT NULL COMMENT 'City name in Chinese',
 PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=81
;

Prepare the CSV file for import:

First, we create a new Excel file with the following content:

Then we convert the excel file to csv file. Operation: File ----> Save as:

Note: Fields are separated by commas by default. The primary key in the document can be left blank, and the order must be consistent with the order of the fields in the database. In addition, we noticed that the encoding format of our database is UTF-8, and our CSV file contains Chinese. If we import it directly, the Chinese may not be imported into the database. We need to change the encoding format of the CSV file to UTF-8. We have a simple way to convert the CSV format. The CSV file can be edited with Notepad. Open it with Notepad for editing, then select some encoding format when saving it.

Import Command

Copy the code as follows:
LOAD DATA INFILE 'D:\\Document\\Download\\test0142.csv' INTO TABLE city_china FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS;

LOAD DATA INFILE is followed by the file path to be imported, INTO TABLE table name FIELDS TERMINATED BY field separation method (the default is comma , which can be changed to other separation methods) ENCLOSED BY semicolon separation, the default is line LINES TERMINATED BY line break IGNORE 1 ROWS ignore the first row (in this example, the first row is the corresponding field in the database. If the first row in your file is data, do not ignore the first row).

MySql Client

Here I use the HeiDiSql client as an example. After connecting to the database, there is a button on the upper left menu (to the left of the two little men), as shown in the figure:


Click and the following page will pop up:

Click Import and then OK.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Navicat imports csv data into mysql
  • Navicat for MySql Visual Import CSV File
  • Import csv file into mysql using navicat
  • MySQL implements query results export csv file and import csv file into database operation
  • Python csv file import and export method from MySQL database
  • How to import csv files into mysql database using PHP programming
  • Python implements multi-process import of CSV data to MySQL
  • How to import csv file into mysql database using php
  • How to solve the problem of unsuccessful import of csv data into mysql using SQLyog
  • How to solve the problem of Chinese garbled characters when importing and exporting csv in Mysql

<<:  How to view the creation time of files in Linux

>>:  Solution to click event failure when using better-scroll on vue mobile development

Recommend

Example code for hiding element scrollbars using CSS

How can I hide the scrollbars while still being a...

Solve the margin: top collapse problem in CCS

The HTML structure is as follows: The CCS structu...

Commonplace talk about the usage of MYSQL pattern matching REGEXP and like

like LIKE requires the entire data to match, whil...

Summary of Docker configuration container location and tips

Tips for using Docker 1. Clean up all stopped doc...

Implementation of Docker deployment of Django+Mysql+Redis+Gunicorn+Nginx

I. Introduction Docker technology is very popular...

Detailed tutorial on installing Python 3 virtual environment in Ubuntu 20.04

The following are all performed on my virtual mac...

How to write DROP TABLE in different databases

How to write DROP TABLE in different databases 1....

Detailed steps for configuring Tomcat server in IDEA 2020

The steps for configuring Tomcat in IDEA 2020 are...

Detailed explanation of Vue router routing guard

Table of contents 1. Global beforeEach 1. Global ...

How to install MySQL via SSH on a CentOS VPS

Type yum install mysql-server Press Y to continue...

How to update, package, and upload Docker containers to Alibaba Cloud

This time, we will try to package the running con...

Troubleshooting ideas and solutions for high CPU usage in Linux systems

Preface As Linux operation and maintenance engine...

How to change password in MySQL 5.7.18

How to change the password in MySQL 5.7.18: 1. Fi...

Compatibility with the inline-block property

<br />A year ago, there were no articles abo...