| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |
|

10-21-11, 07:43
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 2
|
|
|
LOAD DATA LOCAL Timestamps
|
|
Hi all,
I am using:
LOAD DATA LOCAL INFILE '/tmp/data2.txt' INTO TABLE dataTable FIELDS TERMINATED BY '|' LINES TERMINATED BY '\n';
to upload delimited data to my database. Unfortunately though it uses the default timestamp of 0000-00-00 00:00:00 for my Timestamp data and not what is in the file. How can I get it to upload the Timestamp data from the file?
The structure is as follows:
TIMESTAMP|CONVERSION_ID|CLIENT_ID|Event TIMESTAMP
Many Thanks!
|
|

10-21-11, 08:51
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
depends what you mean by timestamp
i would load the data into a "holding" table which has columns that match the exact format of the data
then once it's loaded, i would use INSERT SELECT to copy and transform the data into its final table
in the SELECT part of the INSERT SELECT, i would use the STR_TO_DATE function to convert the datetime value, or else FROM_UNIXTIME if by "timestamp" you meant a unix epoch integer
|
|

10-21-11, 09:05
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 2
|
|
|
|
Hi,
Its in the format: 20111010 23:55:38 in the data file.
While the database stores dates in the format of: 2011-09-13 15:05:07
a holding table would be impractical because this is something that will have to be done daily by some really untech savvy work colleagues.
Thank you,
|
|

10-21-11, 09:20
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
sorry to hear that it will be impractical
by the way, mysql does not store dates in the format of: 2011-09-13 15:05:07
that is indeed one of the formats that allows you to enter datetime values into mysql, but the internal stored format of datetime values is completely different
|
|

10-21-11, 11:56
|
|
Registered User
|
|
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
|
|
|
|

10-21-11, 12:50
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
nice tutorial, very clearly written
i did not know about SET in LOAD DATA :blush:
in this case, there is no IF or variable required, just use STR_TO_DATE with a format that acknowledges that the year-month-day separators are missing

|
|

11-04-11, 22:49
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 15
|
|
hi
very good tutorial, but now what if i want to load data such in the table below and imTypeID is a foreign key which represent learning materials type such
1. Tutorial
2. Exercise
3. Case Study
i tried to load the data and it returns error that it cant load foreign key, when i remove the imTypeID column, data is loaded. is there is a way to resolve this problem. your advice is highly appreciated.
+-----+-----------------+-------------+---------------+-------+------------+
| imID | imTitle | imCreator | imDate | imURL | imTypeID |
+-----+-----------------+-------------+---------------+-------+------------+
| 1 | Arrays1 | Tom Thumb | 2011-08-21 | www | 1 |
| 2 | Arrays2 | Tiny Tim | 2008-01-23 | www | 2 |
| 3 | Arrays3 | Donald Duck | 2011-05-28 | www | 3 |
+-----+-----------------+-------------+---------------+-------+------------+
thanks
regards
|
Last edited by zbath81; 11-04-11 at 23:04.
|

11-05-11, 07:01
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
Quote:
Originally Posted by zbath81
i tried to load the data and it returns error that it cant load foreign key
|
make sure the table being referenced by the imTypeID column actually has rows with those values
|
|

11-05-11, 08:10
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 15
|
|
Quote:
Originally Posted by r937
make sure the table being referenced by the imTypeID column actually has rows with those values
|
do you mean that the foreign key column has entries?
yes it has and still i get error, i tried to switch off foreign key constraint checking before loading the data and then switching it back on again after loading the data, as shown in the syntax below.....
SET FOREIGN_KEY_CHECKS = 0;
LOAD DATA LOCAL INFILE 'C:\localhost\test.csv'
INTO TABLE instructionalMaterials
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(imID,imTitle,imCreator,imDate,imURL,imTypeID);
SET FOREIGN_KEY_CHECKS = 1;
the data was loaded but the foreign key columns returns all 0 values.
any advice...
best regards
zellal
|
|

11-05-11, 09:21
|
|
Registered User
|
|
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
|
|
I have a few questions about this:
a) Does you test.csv have entries for the field imTypeID?
b) Does the field imTypeID have a default which is 0?
c) Can you provide sample script so that this can be tested?
I think that the issue may be you have a default value of 0 which of course needs to have a corresponding entry in the parent table. It also suggests that the field imTypeID is not being populated due to it not existing in the csv file.
|
|

11-06-11, 04:08
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 15
|
|
this is the parent table for materials type
+----------+------------+
| imTypeID | imType |
+----------+-------------
| 1 | Tutorial |
| 2 | Exercise |
| 3 | Case Study |
+---------+-------------+
and yes the field imTypeID in the test.csv file has entries
and this is the syntax i use to load the data
LOAD DATA LOCAL INFILE 'C:\localhost\test.csv'
INTO TABLE instructionalMaterials
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(imID,imTitle,imCreator,imDate,imURL,imTypeID);
attached below a screenshot of my entries file
|
|

11-06-11, 05:17
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
please run this to verify something...
Code:
SELECT imTypeID
, COUNT(*)
FROM instructionalMaterials
GROUP
BY imTypeID
|
|

11-06-11, 16:24
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 15
|
|
this is what i got...
+----------+----------+
| imTypeID | COUNT(*) |
+----------+----------+
| 1 | 16 |
| 2 | 8 |
+---------+-----------+
i dont understand what does that mean 
|
|

11-06-11, 16:47
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
okay, now please run the same query on your materials type table
|
|

11-06-11, 17:08
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 15
|
|
+----------+----------+
| imTypeID | COUNT(*) |
+----------+----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
+---------+-----------+
this is what i got...
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|