Allen 2010-11-12 23:12:28 9836 0 0 0 0

MySql使用外部文件:调用脚本及导入外部数据文件

 
1,调用外部SQl脚本文件
 
建立文件:c: 1.txt如下:
use test;
create table teacher
(
id int(3) auto_increment not null primary key,
name char(10) not null,
address varchar(50) default '深圳',
year date
); 
 
insert into teacher (name,address,year) values('glchengang','深圳一中','1976-10-10');
insert into teacher (name,address,year) values('jack','深圳一中','1975-12-23');
 
在dos下运行:mysql -uroot -pabc < c: 1.txt
 
如果成功,空出一行无任何显示;如有错误,会有提示。
 
如果想看结果,而输出结果很多,例如:
use mysq;
select * from user;
 
则可以用这样的命令: 
mysql < mytest.sql | more 
 
我们还可以将结果输出到一个文件中: 
mysql < mytest.sql > mytest.text
 
+++
2,导入文本数据转到数据库中
语法:load data local infile "文件名" into table 表名;
 
--导入文本方式将数据装入一个数据库表 
创建一个文本文件“t1.txt”,每行包含一个记录,用定位符(tab)把值分开,并且以在CREATE TABLE语句中列出的列次序给出,例如: 
3 rose 深圳二中 1976-10-10
4 mike 深圳一中 1975-12-23
5 tom null 1975-12-23
 
mysql> load data local infile "c:/t1.txt" into table teacher;
Query OK, 3 rows affected (0.00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0
 
注意:
如果要指定导入文本文件在%basedir%in下,可以直接指定此文件。 如:load data local infile "t1.txt" into table teacher;
否则,需要指定它的绝对路径时,此时特别注意:windows下目录中不能用常用的斜杠"",要用反斜杠“/” ,否则会提示找不到文件。


【版權聲明】
本文爲原創,遵循CC 4.0 BY-SA版權協議!轉載時請附上原文鏈接及本聲明。
原文鏈接:https://tdlib.com/am.php?t=ZxZ6241Wpu79
Tag: Mysql 笔记 TTTBLOG
我也要發一個   ·   返回首頁   ·   返回[Mysql]   ·   前一個   ·   下一個
歡迎評論
未登錄,
請先 [ 註冊 ] or [ 登錄 ]
(一分鍾即可完成註冊!)
返回首頁     ·   返回[Mysql]   ·   返回頂部