Loading Data into a Table;MySQL从本地向数据库导入数据

时间:2023-08-06 15:30:50
  • 在localhost中准备好了一个test数据库和一个pet表:
    1. mysql> SHOW DATABASES;
      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | test               |
      +--------------------+
       rows in set (0.01 sec)
      
      mysql> USE test
      Database changed
      mysql> SHOW TABLES;
      +----------------+
      | Tables_in_test |
      +----------------+
      | pet            |
      +----------------+
       row in set (0.01 sec)
  • 在电脑中准备一个pet.txt文件,里面包含想要导入到pet表的数据。(文件中一行代表一条数据,一条数据中的属性用Tab键隔开)
  • 向pet表载入pet.txt中的数据:
    1. mysql> LOAD DATA LOCAL INFILE 'E:\Desktop\pet.txt' INTO TABLE pet;
      ERROR  (): The used command is not allowed with this MySQL version

      ERROR原因:服务器端,local_infile默认开启,客户端local_infile默认关闭,因此用的时候需要打开它

    2. mysql> SHOW VARIABLES like 'local_infile';
      +---------------+-------+
      | Variable_name | Value |
      +---------------+-------+
      | local_infile  | OFF   |
      +---------------+-------+
       row  warning (0.04 sec)

      开启local_infile:(开启后,再次执行SHOW VARIABLES like 'local_infile'会看到Value为ON)

    3. mysql> SET GLOBAL local_infile=ON;
      Query OK,  rows affected (0.00 sec)

      重新载入:

    4. mysql> LOAD DATA LOCAL INFILE 'E:\Desktop\pet.txt' INTO TABLE pet;
      ERROR  (HY000):  - No such file or directory)

      ERROR原因:路径应使用'/'   , 重新载入:

    5. mysql> LOAD DATA LOCAL INFILE 'E:/Desktop/pet.txt' INTO TABLE pet;
      Query OK,  rows affected,  warnings (0.02 sec)
      Records:   Deleted:   Skipped:   Warnings:   

      载入完毕,查看载入成功后的pet表: ψ(`∇´)ψ

    6. mysql> select * from pet;
      +----------+--------+---------+------+------------+------------+
      | name     | owner  | species | sex  | birth      | death      |
      +----------+--------+---------+------+------------+------------+
        |
        |
        |
        |
        |
        |
        |
        |
      +----------+--------+---------+------+------------+------------+
       rows in set (0.00 sec