2

I have a CSV file with the timestamp in the following format

 timestamp,             day_chan2, day_chan3
01/02/2014 00:00,             9,    2
01/02/2014 00:00,            16,    5

I am trying to import it into a MySQL database using LOAD DATA INFILE

$query_name = "LOAD DATA INFILE ' "
                                . $file_path . 
                                "' INTO TABLE '"
                                . $this->table_name . 
                                 " ' FIELDS TERMINATED BY '\,' 
                                 LINES TERMINATED BY '\\n' 
                                 IGNORE 1 LINES 
                                 (`time_stamp`,`day_chan2`,`day_chan3`)";

My problem is the following: how do I convert the timestamp into a format acceptable to my MySQL while importing it into the database?

I am clueless now on how to change the timestamp into a proper timestamp which I can use to query later.

1
  • you wont be able to use LOAD DATA INFILE as you need to run a conversiton on the date field, you will have to loop thought it line by line Commented May 7, 2014 at 20:46

2 Answers 2

3

I've never tried this, but you might be able to do something like:

$query_name = "LOAD DATA INFILE ' "
                                . $file_path . 
                                "' INTO TABLE '"
                                . $this->table_name . 
                                 " ' FIELDS TERMINATED BY '\,' 
                                 LINES TERMINATED BY '\\n' 
                                 IGNORE 1 LINES 
                                 (@mytimestamp,`day_chan2`,`day_chan3`)
                                 SET time_stamp=STR_TO_DATE(@mytimestamp, '%d/%m/%Y %h:%i');"

There are examples of this in the MySQL documentation:

http://dev.mysql.com/doc/refman/5.1/en/load-data.html

Sign up to request clarification or add additional context in comments.

20 Comments

I'm actually really curious to find out if it works :)
you can do that? neat !
@Dagon I don't actually know. But that's what it seems like in the documentation :) If it doesn't work exactly as above, it might work similarly.
i never post answers i haven't actully tested.
I'd reformat the dates in PHP before posting to mysql... Please see my answer for sample code
|
0

MySQLs STR_TO_DATE is probably the best way to do this, given your comment, see below...

if (!($stmt = $mysqli->prepare("INSERT INTO ". $this->table_name . "(`time_stamp`,`day_chan2`,`day_chan3`) VALUES (STR_TO_DATE(?, 'd/m/y H:M'),?,?)"))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
for ($fields in $data) {
   $stmt->bind_param('i', $fields[0]);
   $stmt->bind_param('i', $fields[1]);
   $stmt->bind_param('i', $fields[2]);
   if (!$stmt->execute()) {
      echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
   }
}

Good luck and leave a comment if you need further help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.