文件的导入与导出

1、数据导入
  1、作用 :把文件系统的内容导入到数据库中
  2、语法
    load data infile "/var/lib/mysql-files/文件名"
    into table 表名
    fields terminated by "分隔符"
    lines terminated by "\n";
  3、将scoretable.csv文件导入到数据库的表中
    1、在数据库中创建对应的表
      create table scoretab(
      id int,
      name varchar(15),
      score float(5,2),
      number bigint,
      class char(7)
      );
    2、把文件拷贝到数据库的默认搜索路径中
      1、查看默认搜索路径
        show variables like "secure_file_priv";
        /var/lib/mysql-files/
      2、拷贝文件
        sudo cp ~/scoretable.csv /var/lib/mysql-files/
      3、执行数据导入语句
        load data infile "/var/lib/mysql-files/scoretable.csv"
        into table scoretab
        fields terminated by ","
        lines terminated by "\n";
        只要有规律的都可以导入

2、数据导出
  1、作用
    将数据库中表的记录导出到系统文件里
  2、语法格式
    select ... from 表名
    into outfile "/var/lib/mysql-files/文件名"
    fields terminated by "分隔符"
    lines terminated by "\n";
  3、把MOSHOU库下的sanguo表英雄的姓名、攻击值、国家导出来,sanguo.txt
    select name,gongji,country from MOSHOU.sanguo
    into outfile "/var/lib/mysql-files/sanguo.txt"
    fields terminated by " "
    lines terminated by "\n";
    $ sudo -i
    $ cd /var/lib/mysql-files/
    $ ls
    $ cat sanguo.txt
  4、将mysql库下的user表中 user、host两个字段的值导出到 user.txt
    select user,host from mysql.user
    into outfile "/var/lib/mysql-files/user.txt" fields terminated by " "
    lines terminated by "\n";

猜你喜欢

转载自www.cnblogs.com/zengsf/p/9573268.html