Home > Database > Mysql Tutorial > How to import txt file in mysql?

How to import txt file in mysql?

青灯夜游
Release: 2019-06-01 15:02:15
Original
18118 people have browsed it

Sometimes when we use mysql database, we want to import a txt text document. How to do it? The following article will introduce the method to you, I hope it will be helpful to you.

How to import txt file in mysql?

1. Prepare data and create a test table

1. Prepare the txt text document that needs to be imported. The path is: c :/data.txt.

How to import txt file in mysql?

Use the Tab key to separate each item. If the field is NULL, use /N to indicate it.

2. Create a new database and a table. The fields designed in the table need to correspond to the number of columns in the imported text document.

Example: Create a table representing student information for testing. The fields include id, name, age, city, and salary. Id and name cannot be empty.

create table person(
id int not null auto_increment,
name varchar(40) not null,
city varchar(20),
salary int,
primary key(id)
)engine=innodb charset=gb2312;
Copy after login

The screenshot of the created table is as follows:

How to import txt file in mysql?

2. Import data

Enter the command to import:

load data local infile “c:/data.txt”  
into table person(name,age,city,salary);
Copy after login

The screenshot of the imported data is as follows:

How to import txt file in mysql?

where local means local. After execution, you can see that NULL data is also imported correctly.

3. Export data

Now export this table to a text file: c:/data_out.txt.

select name,age,city,salary  
into outfile “c:/data_out.txt”  
lines terminated by “/r/n”  
from person;
Copy after login

The screenshot of the exported data is as follows:

How to import txt file in mysql?

where lines terminated by “/r/n” means that each line (i.e. each record) uses /r/ n separates, /r/n is the newline character of the window system. The contents of the exported data_out.txt are exactly the same as data.txt.

The above is the detailed content of How to import txt file in mysql?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template