table of Contents
data import
To prepare the data, first create a database named wb and create a table named student. Prepare a student.txt file on Disk F
Use LOAD DATA to import data
Use load data to import data from the F drive to the database
mysql> LOAD DATA LOCAL INFILE 'F:/student.txt' INTO TABLE student;
Query OK, 20 rows affected (0.00 sec)
Records: 20 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from student;
+------------+--------------+--------+------+-------+
| sno | sname | sex | sage | sdept |
+------------+--------------+--------+------+-------+
| 2018001001 | zhangsan | male | 18 | cs |
| 2018001002 | lisi | female | 19 | MA |
| 2018001003 | jack | male | 20 | CS |
| 2018001004 | clinton | male | 21 | IS |
| 2018001005 | trump | male | 19 | IS |
| 2018001006 | putin | male | 20 | CS |
| 2018001007 | starlin | male | 19 | MA |
| 2018001008 | hilery | female | 19 | IS |
| 2018001009 | zhangming | female | 20 | CS |
| 2018001010 | ligang | male | 19 | MA |
| 2018001011 | 令狐冲 | male | 18 | cs |
| 2018001012 | 任盈盈 | female | 19 | MA |
| 2018001013 | 岳不群 | male | 20 | CS |
| 2018001014 | 余沧海 | male | 21 | IS |
| 2018001015 | 林平之 | male | 19 | IS |
| 2018001016 | 岳灵珊 | male | 20 | CS |
| 2018001017 | 朱元璋 | male | 19 | MA |
| 2018001018 | 郑成功 | female | 19 | IS |
| 2018001019 | 爱新觉罗玄烨 | female | 20 | CS |
| 2018001020 | 慈禧 | male | 19 | MA |
+------------+--------------+--------+------+-------+
20 rows in set (0.00 sec)
Of course, you can also customize the export, by setting some parameters
1.FIELDS TERMINATED BY'value': Set the separation character between fields. It
can be single or multiple characters, by default it is a tab character "\t"
2.FIELDS ENCLOSED
BY'value ': Set the enclosing character of the character, only a single character 3.FIELDS ESCAPED BY'value': Set how to write and read special characters, only a single character, that is, set the escape character default to \.
4. LINES STARTING BY'value': Set the character at the beginning of each line of data, which can be single or multiple characters, and no characters are used by default.
5. LINES TERMINATED BY'value': Set the character at the end of each line of data, which can be Single multiple characters, the default is'\n'
Import data using MySQLimport
There is no need to log in to the MySQL client to use this method. The syntax format is: mysqlimport -u root -p 数据库名 文件 [options]
options are optional parameters. Common parameters are as follows:
--fields-terminated-by= 'value':设置字段之间的分隔字符,可以为单个或多个字符,默认情况下为制表符“\t”。
--fields-enclosed-by= 'value':设置字段的包围字符。
--fields-optionally-enclosed-by= 'value':设置字段的包围字符,只能为单个字符,包括CHAR和VERCHAR等字符数据字段。
--fields-escaped-by= 'value':控制如何写入或读取特殊字符,只能为单个字符,即设置转义字符,默认值为反斜线“\”。
--lines-terminated-by= 'value':设置每行数据结尾的字符,可以为单个或多个字符,默认值为“\n”。
--ignore-lines=n:忽视数据文件的前n行。
Data output
To prepare the data, first create a database named wb, create a table named student and insert data.
Use SELECT… INTO OUTFILE statement to export data
Export the student table to the local F drive
mysql> SELECT * FROM student INTO OUTFILE 'F:/student.txt';
Query OK, 20 rows affected (0.00 sec)
Exported
Of course, you can also customize the export, by setting some parameters
1.FIELDS TERMINATED BY'value': Set the separator character between fields. It
can be single or multiple characters. By default, it is a tab character "\t"
2. FIELDS ENCLOSED BY'value': Set the enclosing character of the character, only a single character
3.FIELDS ESCAPED BY'value': Set how to write and read special characters, only a single character, that is, set the escape character default to \.
4. LINES STARTING BY'value': Set the character at the beginning of each line of data, which can be single or multiple characters, and no characters are used by default.
5. LINES TERMINATED BY'value': Set the character at the end of each line of data, which can be Single multiple characters, the default is'\n'
Example:
mysql> SELECT * FROM student INTO OUTFILE 'F:/student.txt'
-> FIELDS TERMINATED BY ',' ENCLOSED BY '"'
-> LINES TERMINATED BY '\r\n';
Query OK, 20 rows affected (0.00 sec)
Export using MySQLdump command
Before that, I learned that MySQLdump can back up data. In fact, MySQLdump can also export plain text files.
Syntax format: You can export plain text files Mysqldump -T path -u root -p 数据库名 表名 [options]
only when the -T parameter is specified. Path indicates the directory
options for exporting data. Common values:
--Fields-terminated-by=value: Set the separator character between fields, which can be single or multiple characters, by default it is the tab character "\t".
--Fields-enclosed-by=value: Set the enclosing characters of the field.
--Fields-optionally-enclosed-by=value: Set the enclosing characters of the field, which can only be a single character, and can only include character data fields such as CHAR and VERCHAR.
--Fields-escaped-by=value: Control how to write or read special characters. It can only be a single character, that is, set the escape character. The default value is the backslash "\".
--Lines-terminated-by=value: Set the character at the end of each line of data, which can be single or multiple characters, and the default value is "\n".
Use the Mysqldump command to export the student table
C:\Users\acer> mysqldump -T F:\ wb student -u root -p
Enter password: ****
At this time, a student.sql and a student.txt file have been generated on the F disk
Export using MySQL command
Syntax format: mysql -u root -p --execute="SELECT语句" 数据库名> filename.txt
Example: Use the mysql command to export the student table
mysql -u root -p --execute="SELECT * FROM student;" wb>F:\student.txt
Enter password: ****
At this time, a student.txt file has been generated, and the parameter -vertical can also be used
MySQL can also export query results to HTML files, just use -html
mysql -u root -p --html --execute="SELECT * FROM student;" wb>F:\student.html
Export as xml file
mysql -u root -p --xml --execute="SELECT * FROM student;" wb>F:\student.xml