tags:

views:

158

answers:

3

There are some other questions where people have problems with timestamp being all zeros. I have checked them and this is not a duplicate.

I declare a table like this:

CREATE TABLE  `my_db`.`my_table` (
  `time_stamp` timestamp NOT NULL,
  `author` varchar() NOT NULL,
  `text` text NOT NULL,
  `md5` int(11) NOT NULL,
  PRIMARY KEY (`time_stamp`)
) ;

I also have a second table which will have a timestamp as primary key and they should have the same value.

Coding in Delphi, I use SELECT CURRENT_TIMESTAMP which returns something like `'19/6/2010 4:56:17 AM' which I then use in an INSERT statement. The INSERT succeeds, but the timestamp is all zer0s.

What am I doing wrong?


kk, here's the INSERT code ...

  sqlCommand := 'INSERT INTO my_db.my_table(time_stamp, author, text, md5) VALUES ("' 
                   + timestamp + 
                    '", "mawg", ' +
                    '"Hello, world"' +
                    0 +
                    '");';
  Result := DoSQlCommandWithNoResultSet(sqlCommand, AdoConnection);

[Edit] Insertion will be extremely low rate, one entry every few weeks or maybe months, so I am happy with timestamp as primary key. I am keeping "versions" of things, so timestamp makes sense to me.

I am begging to think that this is an ADO problem, although I would expect ADO to just "pass through". I don't see any other solution. In a console, the output is "correct", but when run through ADO in Delphi then it is wrong


Hmm, can I specify to MySql how it ought to format its dates?

+2  A: 

Have a look at MySQL date functions. They are very extensive and allow a high flexibility.

Besides all that, I would recommend re-thinking your table structure. A timestap as a primary key is not exactly what you want. When you have high traffic, it CAN happen, that the timestamp is the same. Also if you are saving 2 or more records in a row, the timestamp will be the same. Furthermore, your MD5 column is set to int(11). MD5 hashes use mixed characters, so i would rather go with varchar(32).

CREATE TABLE  `my_db`.`my_table` ( 
  `id` int(11) NOT NULL,
  `timestamp` timestamp NOT NULL,
  `author` varchar(100) NOT NULL,
  `text` text NOT NULL,
  `md5` varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) ;
DrColossos
yes, I did have a look at them. But I have a problem, which is why I amm here :-/ ANd, yes, I have though it though and a timestamp is exactly what I want. I need to capture a "version" and there is no other way to do it.And, yes, timestamp can happen at the same time, but my application is threaded, with mutex, etc At least, I think I know what I am doing ;-)btw, +1 Thanks for replying
Mawg
+1, timestamp as primary key is a bad idea.
hudolejev
@hudolejev - I really need "versions". The timestamp is the only possible (*) key to a first step index table which points to other more "normally indexed" tables. (* I could use some very contrived thing as PK< but only if I jump through hoops. As a Use Case, timestamp is what makes sense for "versions")
Mawg
+2  A: 
Lèse majesté
It should be `HH`, not `H`, so `2010/06/19 04:56:17` is correct.
hudolejev
Well, you're allowed to omit the preceding zero in single-digit months/dates/hours/minutes/seconds. Check the docs again (after the part about illegal dates being converted to 0000-00...). I think it's mainly the order of the date values that's screwing it up.
Lèse majesté
+! "fter reviewing the MySQL documentation, it appears that if your timestamp value is incorrectly formatted" +1 "It should be HH, not H" - that's what I thought ... _*BUT*_ I got the value from `SELECT CURRENT_TIMESTAMP` - can it be a Delphi problem? I Don't see how - it just gets a string back.
Mawg
I'm guessing it's a Delphi issue. Do you have a MySQL client or access to one? Because if you run the query: `SELECT CURRENT_TIMESTAMP;` it actually returns this: `2010-06-19 04:22:12`. And try it with just H:MM:SS. It should work without the 0 padding. Likewise, try the insert by leaving out `time_stamp` altogether. It should work as well.
Lèse majesté
@Lèse majesté I failed to find anything about omitting zeros in MySQL date-related docs, but ok, let it be (:
hudolejev
hudolejev, I apologize. I got confused between the link you posted and the page I was looking at. But I'll link to it in my answer =].
Lèse majesté
+1  A: 

AFAIR 19/6/2010 4:56:17 AM is not a valid date format for MySQL date types. You should convert it to 2010-06-19 04:56:14 (see doc).

hudolejev
+1 The strange thing is that I got that string from MySql itself with `SELECT CURRENT_TIMESTAMP`. Is there any function to "normalize" the timestamp?
Mawg
Maybe `STR_TO_DATE()`? http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date
hudolejev
+1 but why should have to use `STR_TO_DATE()` ? I am just using the retune value of `NOW()`
Mawg
Strange. MySQL function `NOW()` should return a time in valid format. If you are retrieving that value from query result with some Delphi function, I guess it is it who corrupts the time format. You could check that by printing the raw query result before parsing it.
hudolejev