Load Data InFile是用于批量向数据表中导入记录。
首先创建一个表
Use Test;
Create Table TableTest(
`ID` mediumint(8) default '0',
`Name` varchar(100) default ''
) TYPE=MyISAM;
向数据表导入数据
Load Data InFile 'C:/Data.txt' Into Table `TableTest`文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/52477.html
常用如下:
Load Data InFile 'C:/Data.txt' Into Table `TableTest` Lines Terminated By '\r\n';
这个语句,字段默认用制表符隔开,每条记录用换行符隔开,在Windows下换行符为“\r\n”文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/52477.html
C:/Data.txt 文件内容如下面两行:
1 A
2 B
“1”和“A”之间有一个制表符
这样就导进两条记录了。文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/52477.html
自定义语法
Load Data InFile 'C:/Data.txt' Into Table `TableTest` Fields Terminated By ',' Enclosed By '"' Escaped By '"' Lines Terminated By '\r\n';文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/52477.html
Fields Terminated By ',' Enclosed By '"' Escaped By '"'
表示每个字段用逗号分开,内容包含在双引号内文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/52477.html
Lines Terminated By '\r\n';
表示每条数据用换行符分开文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/52477.html
和 Load Data InFile 相反的是
Select * From `TableTest` Into OutFile 'C:/Data_OutFile.txt';
把表的数据导出文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/52477.html
评论