Home Database Mysql Tutorial MySQL 文本文件的导入导出数据的方法_MySQL

MySQL 文本文件的导入导出数据的方法_MySQL

Jun 01, 2016 pm 01:19 PM
insert text file

bitsCN.com MySQL写入数据通常用insert语句,如

insert into person values(张三,20),(李四,21),(王五,70)…;

但有时为了更快速地插入大批量数据或交换数据,需要从文本中导入数据或导出数据到文本。
一、 建立测试表,准备数据
首先建立一个用于测试的表示学生信息的表,字段有id、姓名、年龄、城市、薪水。Id和姓名不
能为空。

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;

创建表截图如下:
1 
接着写一个用于导入的文本文件:c:/data.txt。
张三 31 北京 3000
李四 25 杭州 4000
王五 45 /N 4500
小明 29 天津 /N
3 
每一项之间用Tab键进行分隔,如果该字段为NULL,则用/N表示。
二、 导入数据
输入命令,进行导入。
load data local infile “c:/data.txt”
into table person(name,age,city,salary);
导入数据截图如下:
2 
其中local表示本地。执行后,可以看到NULL数据也被正确地导入。
三、 导出数据

现在将这个表导出为文本文件:c:/data_out.txt。

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

导出数据截图如下:
4 
其中lines terminated by “/r/n”表示每一行(即每一条记录)用/r/n分隔,/r/n是window系
统的换行符。导出的data_out.txt与data.txt的内容完全一样。
四、 运行环境

Windows vista home basic
MySQL 5.1.34-community
五、 注意

字段之间的分隔和记录(行)之间的分隔默认是/t(即Tab)和/n。但可以改变,如:
FIELDS TERMINATED BY ',' --字段用,进行分隔
LINES TERMINATED BY ';' --记录用; 进行分隔
另外要注意其它操作系统的换行符与windows可能不相同。bitsCN.com

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
What is the difference between insert ignore, insert and replace in mysql What is the difference between insert ignore, insert and replace in mysql May 29, 2023 pm 04:40 PM

The difference between insertignore, insert and replace instructions already exist or not. Example of insert error. Insertintonames(name,age)values("Xiao Ming", 23); insertignore ignores insertignoreintonames(name, age)values("Xiao Ming", 24); replace Replace and insert replaceintonames(name,age)values("Xiao Ming", 25); table requirements: PrimaryKey, or unique index result: the table id will be automatically incremented. Test code creates table

How to read text files in html How to read text files in html Mar 26, 2024 pm 04:07 PM

HTML itself cannot read text files directly, but this functionality can be achieved through back-end programming languages ​​(such as PHP, Python, Java) or front-end JavaScript technology. The backend method uses PHP's file_get_contents() function to read the content from the text file and embed it into the HTML page. The front-end JavaScript method uses the Fetch API to send a GET request to a text file on the server, then parses the response content and displays it in an HTML page.

Use java's StringBuilder.insert() function to insert a string at the specified position Use java's StringBuilder.insert() function to insert a string at the specified position Jul 24, 2023 pm 09:37 PM

Use java's StringBuilder.insert() function to insert a string at a specified position. StringBuilder is a class in Java used to handle variable strings. It provides a variety of methods to operate strings. The insert() function is used to insert strings at specified positions. One of the common methods of positionally inserting strings. In this article, we will introduce how to use the insert() function to insert a string at a specified position and give corresponding code examples. insert()

How to add, edit and delete table rows in jQuery? How to add, edit and delete table rows in jQuery? Sep 05, 2023 pm 09:49 PM

In today's era of web development, effective and efficient table management has become very important, especially when dealing with data-heavy web applications. The ability to dynamically add, edit, and delete rows from a table can significantly enhance the user experience and make applications more interactive. An effective way to achieve this is to leverage the power of jQuery. jQuery provides many features to help developers perform operations. Table rows A table row is a collection of interrelated data, represented by elements in HTML. It is used to group together cells (represented by elements) in a table. Each element is used to define a row in the table, and for multi-attribute tables, it usually contains one or more elements. Syntax$(selector).append(co

What is the extension of text file What is the extension of text file Aug 22, 2022 pm 01:59 PM

The extension of text files is "txt". Text files are files with the TXT suffix and contain very little format information. The ".txt" format is not clearly defined. It usually refers to those formats that can be accepted by the system terminal or a simple text editor; any program that can read text can read files with the ".txt" extension. files, therefore, are generally considered to be universal and cross-platform.

Best practices for reading text files with HTML Best practices for reading text files with HTML Apr 09, 2024 pm 03:45 PM

Text files can be read via HTML using the FileReader API. Best practices include filtering file types using the accept attribute, selecting multiple files using the multiple attribute, and reading files through the onchange event handler. A practical case demonstrates how to read a text file and display its content, using the readAsText() method of FileReader to load the file content into a variable.

In C language, what are text files and binary files? In C language, what are text files and binary files? Sep 08, 2023 pm 04:37 PM

A file is a collection of records (or) a place on a hard disk where data is permanently stored. There are two types of file languages ​​in File Type C as follows - Text file Binary file Text file It contains letters and numbers that are easily understandable by humans. Errors in text files can be eliminated under the following conditions: In text files, text and characters are stored one character per byte. For example the integer value 4567 will occupy 2 bytes in memory but 5 bytes in the text file. Data formats are usually row-oriented. Here, each line is a separate command. Binary file contains 1's and 0's and is easily understood by computers. Errors in binary files can corrupt the file and are difficult to detect. In binary file, integer value 1245 will occupy 2 bytes in memory and file

insert statement insert statement Sep 15, 2023 pm 01:30 PM

The basic syntax of the insert statement is "INSERT INTO table name (column 1, column 2, column 3, ...), VALUES (value 1, value 2, value 3, ...);", "table name" is to be inserted The name of the data table. "Column 1", "Column 2", "Column 3", etc. are the names of the columns in the table where data is to be inserted. "Value 1", "Value 2", "Value 3", etc. are the names of the columns to be inserted. data value.

See all articles