Nếu bạn làm echo[$sql];
trước khi thực hiện nó, bạn sẽ thấy cú pháp của truy vấn của bạn không chính xác vì lý do sau:
Tên tệp nên được đặt trong các trích dẫn thay vì backticks vì đó là một chuỗi theo nghĩa đen không phải là một định danh.
Hoàn toàn không cần phải gọi
mysql_escape_string[]
để chỉ định một dấu phân cách trong các mệnh đềFIELDS TERMINATED BY
vàENCLOSED BY
vàESCAPED BY
.Bạn lạm dụng các backticks. Trong thực tế trong trường hợp của bạn, vì không có từ dành riêng được sử dụng, bạn sẽ bỏ tất cả chúng. Họ chỉ thêm sự lộn xộn.
Vào cuối dòng đầu tiên của tệp CSV của bạn, bạn phải có
,,,
vì bạn sử dụng chúng như một phần của dấu phân cách dòng. Nếu bạn không làm điều đó, bạn sẽ bỏ qua không chỉ dòng đầu tiên mà còn là thứ hai có chứa dữ liệu.have to have,,,
because you use them as part of a line delimiter. If you won't do that you'll skip not only first line but also second one that contains data.Bạn không thể sử dụng mệnh đề
ENCLOSED BY
nhiều hơn một lần. Bạn phải đối phó với trường
1 theo một cách khác.LOAD DATA INFILE 'detection.csv' INTO TABLE calldetections FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',,,\r\n' IGNORE 1 LINES [@date, name, type, @number, @duration, @addr, @pin, @city, @state, @country, lat, log] SET date = STR_TO_DATE[@date, '%b-%d-%Y %h:%i:%s %p'], number = TRIM[BOTH '\'' FROM @number], duration = 1 * TRIM[TRAILING 'Secs' FROM @duration], addr = NULLIF[@addr, 'null'], pin = NULLIF[@pin, 'null'], city = NULLIF[@city, 'null'], state = NULLIF[@state, 'null'], country = NULLIF[@country, 'null']
Nhìn vào các hàng mẫu của bạn IMHO bạn không cần
ESCAPED BY
. Nhưng nếu bạn cảm thấy như bạn cần nó sử dụng nó như thế này
3.LOAD DATA INFILE 'detection.csv' INTO TABLE calldetections FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',,,\r\n' IGNORE 1 LINES [@date, name, type, @number, @duration, @addr, @pin, @city, @state, @country, lat, log] SET date = STR_TO_DATE[@date, '%b-%d-%Y %h:%i:%s %p'], number = TRIM[BOTH '\'' FROM @number], duration = 1 * TRIM[TRAILING 'Secs' FROM @duration], addr = NULLIF[@addr, 'null'], pin = NULLIF[@pin, 'null'], city = NULLIF[@city, 'null'], state = NULLIF[@state, 'null'], country = NULLIF[@country, 'null']
Điều đó được nói là một tuyên bố chính xác cú pháp có thể trông như thế này
LOAD DATA INFILE 'detection.csv'
INTO TABLE calldetections
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY ',,,\r\n'
IGNORE 1 LINES
[date, name, type, number, duration, addr, pin, city, state, country, lat, log]
Bây giờ IMHO bạn cần chuyển đổi khá nhiều trường trong khi bạn tải chúng:
Nếu
4 trong bảng của bạn thuộc loại dữ liệuLOAD DATA INFILE 'detection.csv' INTO TABLE calldetections FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',,,\r\n' IGNORE 1 LINES [@date, name, type, @number, @duration, @addr, @pin, @city, @state, @country, lat, log] SET date = STR_TO_DATE[@date, '%b-%d-%Y %h:%i:%s %p'], number = TRIM[BOTH '\'' FROM @number], duration = 1 * TRIM[TRAILING 'Secs' FROM @duration], addr = NULLIF[@addr, 'null'], pin = NULLIF[@pin, 'null'], city = NULLIF[@city, 'null'], state = NULLIF[@state, 'null'], country = NULLIF[@country, 'null']
5 thì nó cần được chuyển đổi, nếu không bạn sẽ gặp lỗiLOAD DATA INFILE 'detection.csv' INTO TABLE calldetections FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',,,\r\n' IGNORE 1 LINES [@date, name, type, @number, @duration, @addr, @pin, @city, @state, @country, lat, log] SET date = STR_TO_DATE[@date, '%b-%d-%Y %h:%i:%s %p'], number = TRIM[BOTH '\'' FROM @number], duration = 1 * TRIM[TRAILING 'Secs' FROM @duration], addr = NULLIF[@addr, 'null'], pin = NULLIF[@pin, 'null'], city = NULLIF[@city, 'null'], state = NULLIF[@state, 'null'], country = NULLIF[@country, 'null']
Giá trị DateTime không chính xác: 'Tháng 9-18-2013 01:53:45 PM' cho cột 'ngày' ở hàng
Bạn phải đối phó với các Qoutes đơn xung quanh các giá trị trong trường
1LOAD DATA INFILE 'detection.csv' INTO TABLE calldetections FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',,,\r\n' IGNORE 1 LINES [@date, name, type, @number, @duration, @addr, @pin, @city, @state, @country, lat, log] SET date = STR_TO_DATE[@date, '%b-%d-%Y %h:%i:%s %p'], number = TRIM[BOTH '\'' FROM @number], duration = 1 * TRIM[TRAILING 'Secs' FROM @duration], addr = NULLIF[@addr, 'null'], pin = NULLIF[@pin, 'null'], city = NULLIF[@city, 'null'], state = NULLIF[@state, 'null'], country = NULLIF[@country, 'null']
Bạn rất có thể muốn thay đổi
7 Chuỗi theo nghĩa đen thành thực tếLOAD DATA INFILE 'detection.csv' INTO TABLE calldetections FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',,,\r\n' IGNORE 1 LINES [@date, name, type, @number, @duration, @addr, @pin, @city, @state, @country, lat, log] SET date = STR_TO_DATE[@date, '%b-%d-%Y %h:%i:%s %p'], number = TRIM[BOTH '\'' FROM @number], duration = 1 * TRIM[TRAILING 'Secs' FROM @duration], addr = NULLIF[@addr, 'null'], pin = NULLIF[@pin, 'null'], city = NULLIF[@city, 'null'], state = NULLIF[@state, 'null'], country = NULLIF[@country, 'null']
8 cho các cộtLOAD DATA INFILE 'detection.csv' INTO TABLE calldetections FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',,,\r\n' IGNORE 1 LINES [@date, name, type, @number, @duration, @addr, @pin, @city, @state, @country, lat, log] SET date = STR_TO_DATE[@date, '%b-%d-%Y %h:%i:%s %p'], number = TRIM[BOTH '\'' FROM @number], duration = 1 * TRIM[TRAILING 'Secs' FROM @duration], addr = NULLIF[@addr, 'null'], pin = NULLIF[@pin, 'null'], city = NULLIF[@city, 'null'], state = NULLIF[@state, 'null'], country = NULLIF[@country, 'null']
9LOAD DATA INFILE 'detection.csv' INTO TABLE calldetections FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',,,\r\n' IGNORE 1 LINES [@date, name, type, @number, @duration, @addr, @pin, @city, @state, @country, lat, log] SET date = STR_TO_DATE[@date, '%b-%d-%Y %h:%i:%s %p'], number = TRIM[BOTH '\'' FROM @number], duration = 1 * TRIM[TRAILING 'Secs' FROM @duration], addr = NULLIF[@addr, 'null'], pin = NULLIF[@pin, 'null'], city = NULLIF[@city, 'null'], state = NULLIF[@state, 'null'], country = NULLIF[@country, 'null']
Nếu thời lượng luôn luôn trong vài giây thì bạn có thể trích xuất giá trị số nguyên của giây và lưu trữ nó theo cách đó trong bảng của bạn để có thể dễ dàng tổng hợp các giá trị thời lượng sau này.
Điều đó được nói là một phiên bản hữu ích của câu lệnh sẽ trông giống như thế này
LOAD DATA INFILE 'detection.csv'
INTO TABLE calldetections
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY ',,,\r\n'
IGNORE 1 LINES
[@date, name, type, @number, @duration, @addr, @pin, @city, @state, @country, lat, log]
SET date = STR_TO_DATE[@date, '%b-%d-%Y %h:%i:%s %p'],
number = TRIM[BOTH '\'' FROM @number],
duration = 1 * TRIM[TRAILING 'Secs' FROM @duration],
addr = NULLIF[@addr, 'null'],
pin = NULLIF[@pin, 'null'],
city = NULLIF[@city, 'null'],
state = NULLIF[@state, 'null'],
country = NULLIF[@country, 'null']
Dưới đây là kết quả của việc thực hiện truy vấn trên máy của tôi
mysql> LOAD DATA INFILE '/tmp/detection.csv' -> INTO TABLE calldetections -> FIELDS TERMINATED BY ',' -> OPTIONALLY ENCLOSED BY '"' -> LINES TERMINATED BY ',,,\n' -> IGNORE 1 LINES -> [@date, name, type, @number, @duration, @addr, @pin, @city, @state, @country, lat, log] -> SET date = STR_TO_DATE[@date, '%b-%d-%Y %h:%i:%s %p'], -> number = TRIM[BOTH '\'' FROM @number], -> duration = 1 * TRIM[TRAILING 'Secs' FROM @duration], -> addr = NULLIF[@addr, 'null'], -> pin = NULLIF[@pin, 'null'], -> city = NULLIF[@city, 'null'], -> state = NULLIF[@state, 'null'], -> country = NULLIF[@country, 'null']; Query OK, 3 rows affected [0.00 sec] Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 mysql> select * from calldetections; +---------------------+---------+---------------+-------------+----------+------+------+------+-------+---------+------+------+ | date | name | type | number | duration | addr | pin | city | state | country | lat | log | +---------------------+---------+---------------+-------------+----------+------+------+------+-------+---------+------+------+ | 2013-09-18 13:53:45 | Unknown | outgoing call | 123456 | 0 | NULL | NULL | NULL | NULL | NULL | 0.0 | 0.0 | | 2013-09-18 13:54:14 | Unknown | outgoing call | 1234567890 | 0 | NULL | NULL | NULL | NULL | NULL | 0.0 | 0.0 | | 2013-09-18 13:54:37 | Unknown | outgoing call | 14772580369 | 1 | NULL | NULL | NULL | NULL | NULL | 0.0 | 0.0 | +---------------------+---------+---------------+-------------+----------+------+------+------+-------+---------+------+------+ 3 rows in set [0.00 sec]
Và cuối cùng trong PHP gán một chuỗi truy vấn cho biến
mysql> LOAD DATA INFILE '/tmp/detection.csv' -> INTO TABLE calldetections -> FIELDS TERMINATED BY ',' -> OPTIONALLY ENCLOSED BY '"' -> LINES TERMINATED BY ',,,\n' -> IGNORE 1 LINES -> [@date, name, type, @number, @duration, @addr, @pin, @city, @state, @country, lat, log] -> SET date = STR_TO_DATE[@date, '%b-%d-%Y %h:%i:%s %p'], -> number = TRIM[BOTH '\'' FROM @number], -> duration = 1 * TRIM[TRAILING 'Secs' FROM @duration], -> addr = NULLIF[@addr, 'null'], -> pin = NULLIF[@pin, 'null'], -> city = NULLIF[@city, 'null'], -> state = NULLIF[@state, 'null'], -> country = NULLIF[@country, 'null']; Query OK, 3 rows affected [0.00 sec] Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 mysql> select * from calldetections; +---------------------+---------+---------------+-------------+----------+------+------+------+-------+---------+------+------+ | date | name | type | number | duration | addr | pin | city | state | country | lat | log | +---------------------+---------+---------------+-------------+----------+------+------+------+-------+---------+------+------+ | 2013-09-18 13:53:45 | Unknown | outgoing call | 123456 | 0 | NULL | NULL | NULL | NULL | NULL | 0.0 | 0.0 | | 2013-09-18 13:54:14 | Unknown | outgoing call | 1234567890 | 0 | NULL | NULL | NULL | NULL | NULL | 0.0 | 0.0 | | 2013-09-18 13:54:37 | Unknown | outgoing call | 14772580369 | 1 | NULL | NULL | NULL | NULL | NULL | 0.0 | 0.0 | +---------------------+---------+---------------+-------------+----------+------+------+------+-------+---------+------+------+ 3 rows in set [0.00 sec]0 sẽ trông như thế này
$sql = "LOAD DATA INFILE 'detection.csv'
INTO TABLE calldetections
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY ',,,\\r\\n'
IGNORE 1 LINES
[@date, name, type, @number, @duration, @addr, @pin, @city, @state, @country, lat, log]
SET date = STR_TO_DATE[@date, '%b-%d-%Y %h:%i:%s %p'],
number = TRIM[BOTH '\'' FROM @number],
duration = 1 * TRIM[TRAILING 'Secs' FROM @duration],
addr = NULLIF[@addr, 'null'],
pin = NULLIF[@pin, 'null'],
city = NULLIF[@city, 'null'],
state = NULLIF[@state, 'null'],
country = NULLIF[@country, 'null'] ";