MySQL đọc tập tin
CHỨC NĂNG LOAD_FILE() Show cú pháp LOAD_FILE (file_name); Thông số. Ví dụ mysql> Update stud_table set img = LOAD_FILE('E:\file.jpg'); mysql> Cập nhật stud_table set img = LOAD_FILE('E. \tập tin. jpg'); đầu ra Query OK, 3 rows affected (1.01 sec) Truy vấn OK, 3 hàng bị ảnh hưởng (1. 01 giây) Hãy chia sẻ
Đọc và ghi vào tệp hỗ trợ thu thập dữ liệu cũng như đánh cắp dữ liệu. Nhiều phương pháp bao gồm ghi vào webroot, cho phép thực thi trình bao web hoặc cho phép dữ liệu được trích xuất qua cổng 80/443 Theo mặc định, chương trình mysql đọc đầu vào một cách tương tác từ thiết bị đầu cuối, nhưng bạn có thể cung cấp cho nó các truy vấn ở chế độ hàng loạt bằng cách sử dụng các nguồn đầu vào khác như tệp, chương trình khác hoặc đối số lệnh. Bạn cũng có thể sử dụng sao chép và dán làm nguồn đầu vào truy vấn. Phần này thảo luận về cách đọc các truy vấn từ một tệp. Một vài phần tiếp theo thảo luận về cách lấy thông tin đầu vào từ các nguồn khác Để tạo tập lệnh SQL để mysql thực thi ở chế độ hàng loạt, hãy đặt câu lệnh của bạn vào tệp văn bản, sau đó gọi mysql và chuyển hướng đầu vào của nó để đọc từ tệp đó % Các câu lệnh được đọc từ tệp đầu vào thay thế cho những gì bạn thường nhập bằng tay, vì vậy chúng phải được kết thúc bằng dấu chấm phẩy (hoặc DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);1), giống như khi bạn nhập chúng theo cách thủ công. Một điểm khác biệt giữa chế độ tương tác và hàng loạt là kiểu đầu ra mặc định. Đối với chế độ tương tác, mặc định là dạng bảng (đóng hộp). Đối với chế độ hàng loạt, mặc định là phân định các giá trị cột bằng các tab. Tuy nhiên, bạn có thể chọn bất kỳ kiểu đầu ra nào bạn muốn bằng cách sử dụng các tùy chọn dòng lệnh thích hợp. Xem phần chọn định dạng bảng hoặc định dạng được phân cách bằng tab ở phần sau của chương (Công thức 1. 22) Chế độ hàng loạt thuận tiện khi bạn cần đưa ra một tập hợp các câu lệnh nhất định nhiều lần, bởi vì sau đó bạn không cần phải nhập chúng theo cách thủ công mỗi lần. Ví dụ: chế độ hàng loạt giúp dễ dàng thiết lập các công việc định kỳ chạy mà không cần sự can thiệp của người dùng. Tập lệnh SQL cũng hữu ích để phân phối truy vấn cho người khác. Nhiều ví dụ hiển thị trong cuốn sách này có thể được chạy bằng cách sử dụng các tệp tập lệnh có sẵn như một phần của phân phối nguồn DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);2 đi kèm (xem Phụ lục A). Bạn có thể cung cấp các tệp này cho mysql ở chế độ hàng loạt để tránh tự nhập truy vấn. Một ví dụ phổ biến về điều này là khi một ví dụ hiển thị câu lệnh DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);3 DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);4 mô tả giao diện của một bảng cụ thể, bạn sẽ tìm thấy một tệp bó SQL trong bản phân phối có thể được sử dụng để tạo (và có thể tải dữ liệu vào) bảng. Ví dụ, ở đầu chương, các câu lệnh để tạo và điền vào bảng DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);5 đã được hiển thị. Bản phân phối DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);2 bao gồm một tệp DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);7 chứa các câu lệnh để làm điều tương tự. Các tập tin trông như thế này DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL); Để thực thi các câu lệnh trong tệp tập lệnh SQL này ở chế độ hàng loạt, hãy thay đổi thư mục thành thư mục DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);8 của bản phân phối DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);2 nơi đặt các tập lệnh tạo bảng, sau đó chạy lệnh này mysql> Update stud_table set img = LOAD_FILE('E:\file.jpg');4 Bạn sẽ lưu ý rằng tập lệnh chứa một câu lệnh hủy bảng nếu nó tồn tại trước khi tạo lại và tải nó cùng với dữ liệu. Điều đó cho phép bạn thử nghiệm với bảng mà không phải lo lắng về việc thay đổi nội dung của bảng, bởi vì bạn có thể khôi phục bảng về trạng thái cơ sở bất kỳ lúc nào bằng cách chạy lại tập lệnh Lệnh vừa hiển thị minh họa cách chỉ định tệp đầu vào cho mysql trên dòng lệnh. Kể từ MySQL 3. 23. 9, bạn có thể đọc tệp câu lệnh SQL từ bên trong phiên mysql bằng cách sử dụng lệnh DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);0 mysql> Update stud_table set img = LOAD_FILE('E:\file.jpg');41 (hoặc mysql> Update stud_table set img = LOAD_FILE('E:\file.jpg');42 mysql> Update stud_table set img = LOAD_FILE('E:\file.jpg');41, đồng nghĩa). Giả sử tệp tập lệnh SQL mysql> Update stud_table set img = LOAD_FILE('E:\file.jpg');44 chứa các câu lệnh sau Query OK, 3 rows affected (1.01 sec)0 Bạn có thể thực thi tệp đó từ bên trong mysql như sau Query OK, 3 rows affected (1.01 sec)1 Bản thân các tập lệnh SQL có thể bao gồm các lệnh DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);0 hoặc mysql> Update stud_table set img = LOAD_FILE('E:\file.jpg');42 để bao gồm các tập lệnh khác. Điều nguy hiểm của việc này là có thể tạo ra một vòng lặp nguồn. Thông thường, bạn nên cẩn thận để tránh các vòng lặp như vậy, nhưng nếu bạn cảm thấy nghịch ngợm và muốn cố tình tạo một vòng lặp để tìm hiểu xem mysql có thể lồng các tệp đầu vào sâu đến mức nào, thì đây là cách thực hiện. Trước tiên, hãy đưa ra hai câu lệnh sau theo cách thủ công để tạo bảng mysql> Update stud_table set img = LOAD_FILE('E:\file.jpg');47 nhằm theo dõi độ sâu của tệp nguồn và khởi tạo cấp độ lồng nhau thành 0 Query OK, 3 rows affected (1.01 sec)5 Sau đó, tạo tệp script mysql> Update stud_table set img = LOAD_FILE('E:\file.jpg');48 chứa các dòng sau (đảm bảo mỗi dòng kết thúc bằng dấu chấm phẩy) Query OK, 3 rows affected (1.01 sec)7 Cuối cùng, gọi mysql và đưa ra lệnh DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1); INSERT INTO limbs (thing,legs,arms) VALUES('tripod',3,0); INSERT INTO limbs (thing,legs,arms) VALUES('Peg Leg Pete',1,2); INSERT INTO limbs (thing,legs,arms) VALUES('space alien',NULL,NULL);0 để đọc tệp script Query OK, 3 rows affected (1.01 sec)9 Hai câu lệnh đầu tiên trong mysql> Update stud_table set img = LOAD_FILE('E:\file.jpg');48 tăng bộ đếm lồng nhau và hiển thị giá trị Query OK, 3 rows affected (1.01 sec)01 hiện tại. Trong câu lệnh thứ ba, mysql> Update stud_table set img = LOAD_FILE('E:\file.jpg');48 tự tạo nguồn, do đó tạo ra một vòng lặp đầu vào. Bạn sẽ thấy đầu ra tăng vọt, với màn hình bộ đếm tăng dần mỗi lần qua vòng lặp. Cuối cùng, mysql sẽ hết bộ mô tả tệp và dừng lại do lỗi Làm cách nào để đọc tệp CSV trong MySQL?Dưới đây là các bước. . Chuẩn bị tệp CSV để có các trường theo thứ tự như các trường trong bảng MySQL Xóa hàng tiêu đề khỏi CSV (nếu có), để chỉ có dữ liệu trong tệp Vào giao diện phpMyAdmin Chọn bàn ở menu bên trái Nhấp vào nút nhập ở trên cùng Duyệt đến tệp CSV Làm cách nào để sử dụng Load_file trong MySQL?Cách hoạt động của Hàm LOAD_FILE() trong MySQL . cú pháp. Cú pháp diễn ra như thế này. LOAD_FILE(file_name). Ví dụ. Đây là một ví dụ nơi tôi chọn nội dung từ một tệp. CHỌN LOAD_FILE('/data/test. txt') AS Kết quả;. Một ví dụ cơ sở dữ liệu. . Nếu tệp không tồn tại. . Thêm lý do bạn có thể nhận được NULL Hàm MySQL nào cho phép đọc tệp từ hệ thống tệp?Hàm “ load_file ” của MySQL cung cấp cho người dùng cơ sở dữ liệu khả năng tải các tệp từ hệ thống tệp vào một bảng hoặc kết xuất chúng vào . Nếu đường dẫn đã cho tồn tại, nó hoạt động.
Làm cách nào để nhập tệp trong MySQL?Hiển thị hoạt động trên bài đăng này. . Mở dòng lệnh MySQL Nhập đường dẫn của thư mục bin mysql của bạn và nhấn Enter Dán tệp SQL của bạn vào thư mục bin của máy chủ mysql Tạo cơ sở dữ liệu trong MySQL Sử dụng cơ sở dữ liệu cụ thể mà bạn muốn nhập tệp SQL Nhập tên tệp cơ sở dữ liệu nguồn. sql và Nhập |