我当前正在尝试将数据从reloads.csv
加载到数据库中。
但是DATETIME字段被截断了。我非常注意要获得正确的日期格式,例如answer。
当我加载数据时,会出现这种错误:
连接成功 537,496,3,4,4.36186,50.86034,4.372,50.86309,2017-01-01T22:05:55,2017-01-02T05:02:20 248,129,2,4,4.37052,50.86135,4.36254,50.82652,2017-01-01T23:56:45,2017-01-02T05:05:33错误:INSERT INTO重新加载(踏板车,用户,initialLoad,finalLoad,sourceX,sourceY ,destinationX,destinationY,startTime,endTime)值('248','129','2','4','4.37052','50.86135','4.36254','50.82652','2017-01-01 23 :56:45','2017-01-02 05:05:33') 密钥“ PRIMARY”的条目“ 2017-01-01”重复 363,17,1,4,4.36902,50.87497,4.3785,50.87691,2017-01-01T22:00:52,2017-01-02T05:08:34错误:INSERT INTO重新加载(踏板车,用户,initialLoad,finalLoad,sourceX ,sourceY,destinationX,destinationY,startTime,endTime)值('363','17','1','4','4.36902','50.87497','4.3785','50.87691','2017-01- 01 22:00:52','2017-01-02 05:08:34') 密钥“ PRIMARY”的条目“ 2017-01-01”重复 ...
reloads.csv具有以下结构:
scooter, user, initialLoad, finalLoad, sourceX, sourceY, destinationX,
destinationY, startTime, endTime
537,496,3,4,4.36186,50.86034,4.372,50.86309,2017-01-01T22:05:55,2017-01-02T05:02:20
248,129,2,4,4.37052,50.86135,4.36254,50.82652,2017-01-01T23:56:45,2017-01-02T05:05:33
363,17,1,4,4.36902,50.87497,4.3785,50.87691,2017-01-01T22:00:52,2017-01-02T05:08:34
...
我这样创建了Reloads表:
$sql = "CREATE TABLE Reloads (
scooter INT NOT NULL,
user INT NOT NULL,
initialLoad INT,
finalLoad INT,
sourceX FLOAT,
sourceY FLOAT,
destinationX FLOAT,
destinationY FLOAT,
startTime DATETIME NOT NULL,
endTime DATETIME,
PRIMARY KEY (startTime),
FOREIGN KEY (scooter) REFERENCES Scooters(numero),
FOREIGN KEY (user) REFERENCES Rechargeurs(ID)
)";
以下是将负载重新加载到“重新加载”表中的方式:
//INSERT RELOADS IN DB
if ($fh = fopen("../../data2019/reloads.csv", 'r')) {
$line = fgets($fh);//remove first line
$sql = "";
while (!feof($fh)) {
$line = fgets($fh);
echo "<br>" . $line;
//echo $line;
$str_arr = explode(",", $line);
//print_r($str_arr);
if (count($str_arr) == 10) {
$startTime = str_replace("T", " ",$str_arr[8]);
$endTime = str_replace("T", " ",$str_arr[9]);
$sql = "INSERT INTO Reloads (scooter, user, initialLoad, finalLoad, sourceX, sourceY, destinationX, destinationY,
startTime, endTime)
VALUES (
'$str_arr[0]',
'$str_arr[1]',
'$str_arr[2]',
'$str_arr[3]',
'$str_arr[4]',
'$str_arr[5]',
'$str_arr[6]',
'$str_arr[7]',
'$startTime',
'$endTime'
)";
if ($conn->query($sql) === TRUE) {
//echo "\nNew record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
fclose($fh);
}
我对如何调试和解决问题一无所知。在我的数据库中,所有日期时间都以这种形式被截断:2017-01-01
预先感谢
答案 0 :(得分:1)
根据数据,您可以使用substr
和strpos
来获取日期,格式为Y-m-d
$startTime = '2017-01-01T22:05:55';
$endTime = '2017-01-02T05:08:34';
$startTime = substr($startTime,0,strpos($startTime, 'T'));
$endTime = substr($endTime,0,strpos($endTime, 'T'));
您可以将DateTime
用于Y-m-d H:i:s
$startTime = '2017-01-01T22:05:55';
$dateObject = new DateTime($startTime);
$startTime = $dateObject->format('Y-m-d H:i:s');
$endTime = '2017-01-02T05:08:34';
$dateObject = new DateTime($endTime);
$endTime = $dateObject->format('Y-m-d H:i:s');