我正在尝试将数据从文本文件加载到 MySQL 中的表中。
我已经手动创建了表格,现在我想将数据导入其中。
这是我要导入的文件的内容:
INSERT INTO books VALUES ('Executive Orders', 1996, '0-425-15863-2');
INSERT INTO books VALUES ('Forward the Foundation', 1993, '0-553-67507-9');
INSERT INTO books VALUES ('Foundation', 1951, '0-553-80371-9');
INSERT INTO books VALUES ('Foundation and Empire', 1952, '0-553-29337-0');
INSERT INTO books VALUES ('Foundation\'s Edge', 1982, '0-553-29338-9');
INSERT INTO books VALUES ('I, Robot', 1950, '0-553-29438-5');
INSERT INTO books VALUES ('Isaac Asimov: Gold', 1995, '0-06-055652-8');
INSERT INTO books VALUES ('Raibbow Six', 1998, '0-425-17034-9');
INSERT INTO books VALUES ('Roots', 1974, '0-440-17464-3');
INSERT INTO books VALUES ('Second Foundation', 1953, '0-553-29336-2');
INSERT INTO books VALUES ('Teeth of the Tiger', 2003, '0-399-15079-X');
INSERT INTO books VALUES ('The Best of Issac Asimov', 1973, '0-449-2-829-X');
INSERT INTO books VALUES ('The Hobbit', 1937, '0-261-10221-4');
INSERT INTO books VALUES ('The Return of The King', 1955, '0-261-10237-0');
INSERT INTO books VALUES ('The Sum of All Fears', 1991, '0-425-13554-0');
INSERT INTO books VALUES ('The Two Towers', 1954, '0-261-10236-2');
我正在使用此命令导入文件:
LOAD DATA LOCAL INFILE 'C:/Users/myuser/Desktop/library.sql' INTO TABLE books COLUMNS TERMINATED BY '\n';
没有错误,表中的数据如下所示:
MariaDB [library]> select * from books;
+---------------------------+----------------+----------------------+
| Title | year_published | ISBN |
+---------------------------+----------------+----------------------+
| INSERT INTO books VALUES | 0 | INSERT INTO books VA |
| INSERT INTO books VALUES | 0 | INSERT INTO books VA |
| INSERT INTO books VALUES | 0 | INSERT INTO books VA |
| INSERT INTO books VALUES | 0 | INSERT INTO books VA |
| INSERT INTO books VALUES | 0 | INSERT INTO books VA |
| INSERT INTO books VALUES | NULL | NULL |
+---------------------------+----------------+----------------------+
6 rows in set (0.000 sec)
这就是我希望数据看起来像的样子:
MariaDB [library]> select * from books;
+--------------------------+----------------+---------------+
| Title | year_published | ISBN |
+--------------------------+----------------+---------------+
| Executive Orders | 1996 | 0-425-15863-2 |
| Forward the Foundation | 1993 | 0-553-67507-9 |
| Foundation | 1951 | 0-553-80371-9 |
| Foundation and Empire | 1952 | 0-553-29337-0 |
| Foundation's Edge | 1982 | 0-553-29338-9 |
| I, Robot | 1950 | 0-553-29438-5 |
| Isaac Asimov: Gold | 1995 | 0-06-055652-8 |
| Raibbow Six | 1998 | 0-425-17034-9 |
| Roots | 1974 | 0-440-17464-3 |
| Second Foundation | 1953 | 0-553-29336-2 |
| Teeth of the Tiger | 2003 | 0-399-15079-X |
| The Best of Issac Asimov | 1973 | 0-449-2-829-X |
| The Hobbit | 1937 | 0-261-10221-4 |
| The Return of The King | 1955 | 0-261-10237-0 |
| The Sum of All Fears | 1991 | 0-425-13554-0 |
| The Two Towers | 1954 | 0-261-10236-2 |
+--------------------------+----------------+---------------+
16 rows in set (0.000 sec)
我究竟做错了什么?
library.sql似乎有转储文件的内容,在这种情况下,您需要使用 MySQL 恢复而不是LOAD DATA。LOAD DATA 语句将文本文件中的行读入表中。
当您使用 Windows 时,我将举一个例子。
我创建了一个测试数据库和书籍表,将内容保存
library.sql
在我桌面的以下路径中C:\Users\erges\OneDrive\Desktop\fileee\library.sql
,最后在我C:\Program Files\MySQL\MySQL Server 8.0\bin
执行的命令行中的路径中,