1. 程式人生 > >文件的導入與導出

文件的導入與導出

語法 clas load outfile country tab gin reat load data

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";

文件的導入與導出