欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

数据库管理中文件的使用教程

程序员文章站 2023-12-21 11:15:22
从文本文件中读取数据(import) 常用的文本文件:csv(comma separated values)文件,即:以逗号分隔的数值 形式如下: [plain] m000...
从文本文件中读取数据(import)
常用的文本文件:csv(comma separated values)文件,即:以逗号分隔的数值
形式如下
[plain]
m0001,李刚,1976-01-05,1
m0002,王二,1955-01-15,1
m0003,李四,1967-03-05,1
[sql]
load data infile 'd:/mycodes/test.cvs' into table member fields terminated by ','; -- 注意 test.cvs 文件的编码
select * into outfile 'd:/mycodes/out.cvs' fields terminated by ',' from member;
执行文件中保存的 sql 命令
[sql]
source d:/mycodes/test.sql -- source 并不 sql 命令,因此,结尾不用加分号 ;
mysql test -uroot -p -e "source d:/mycodes/test.sql" -- test 是数据库名
可以将上述 sql 语句写成批处理文件,如:
[plain]
mysql test -uroot -p -e "source d:/mycodes/test.sql"
pause
将 sql 执行结果保存到文件中
键盘、鼠标等输入设备,被称为标准输入;
显示器等设备,属于标准输出。
标准输入、标准输出,这些设备,是可以变更的,这种变更操作就称为重定向(redirect)。
命令窗口中
[sql]
复制代码 代码如下:

dir > d:/mycodes/redirect.txt
dir > d:\mycodes\redirect.txt
type d:\mycodes\redirect.txt
help
help type
mysql 中,

[sql]
复制代码 代码如下:

mysql -uroot -p > d:\mycodes\log.txt
type d:\mycodes\log.txt
mysql -uroot -p -e "source d:/mycodes/test.sql" > d:\mycodes\log.txt

使用 tee 命令将 sql 语句的执行结果保存到文件中
[sql]
复制代码 代码如下:

tee d:/mycodes/teelog.txt
use home;
select * from customer;
notee;
exit;
type d:\mycodes\teelog.txt

数据库备份与恢复
将数据库整体保存到文件中的操作,被称为转储(dump)
将转储文本文件还原成数据库的操作,被称为恢复(restore)
[sql]
复制代码 代码如下:

mysqldump -u root -p home > d:/mycodes/home_back.sql --default-character-set=utf8
mysqladmin -u root -p create home1
mysql -u root -p home1 < d:/mycodes/home_back.sql --default-character-set=utf8

上一篇:

下一篇: