简体   繁体   中英

Load Data Local Infile with Date

I am trying to insert the current date and time to through a Load Data Local Inline statement. Would like to insert the current date and time through the command. Getting the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@Date_Update=date('Ym-d')' at line 3.

Here is my statement.

 $sqlstatement="LOAD DATA LOCAL INFILE '$temp' INTO TABLE contacts 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '\r' 
IGNORE 1 LINES
    (User_Category, Name, Logon_id, @Date_Update)
    SET ID=@ID, @Date_Update=date('Y-m-d');
    ";

Is setting the current date possible wih Load Data Local Infile?

I think the problem is the wrong assignation of the fields :

$sqlstatement="LOAD DATA LOCAL INFILE '$temp' INTO TABLE contacts 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '\r' 
IGNORE 1 LINES
    (User_Category, Name, @Logon_id, @Date_Update)     ◄■■■■■■■■■■■■■■■■
    SET ID=@Logon_id, Date_Update=NOW();               ◄■■■■■■■■■■■■■■■■
    ";

You don't assign to @variable in the SET clause. That should contain the table column you want to assign to, using @variable to get the column from the CSV file.

But if you want to insert the current date, you don't need to access a CSV column.

$sqlstatement="LOAD DATA LOCAL INFILE '$temp' INTO TABLE contacts 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '\r' 
IGNORE 1 LINES
    (User_Category, Name, Logon_id, @Date_Update)
    SET ID=@ID, Date_Update=TODAY();
    ";

BTW, date('Ym-d') is a PHP function, not MySQL. The MySQL function for formatting dates and times is DATE_FORMAT() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM