Q:

Time as key into SQL causes error

I'm trying to fix a bug regarding SQL (used in Android), the statement basically looks like this:
INSERT INTO log (time, message) VALUES (datetime('now'),<a_message>);

The "datetime(now)" seems to be the key into the database, and the problem regarding it is that it can be called 2 times within one millisecond.  If that happens, an error in SQL will be thrown since the key is not unique. 
I could use REPLACE into, but that would overwrite one (or more) entry's.  I could also check the error code, sleep some time and try again.  I'm just wondering if anyone have a better solution for this problem?  I'm not very experienced with SQL. 
Thanks. 

A:

You should be much more hesitant to use the datetime as a primary key anyway because of the wild and wooly world of timezones, leapseconds, and lots of other things that will surprise you.   There is absolutely no guarantee that a1 = datetime('now') is always less than or equal to the next invocation of a2 = datetime('now')
Read this article, about 1/3 the way down he starts talking about dates, times, and timezones.   This should convince you that you should not be trying to do what you are asking for in this question:
https://msmvps.com/blogs/jon_skeet/archive/2009/11/02/omg-ponies-aka-humanity-epic-fail.aspx
If I wanted a quick and dirty hack work around, I would put the insert into a while loop and catch the error when the insert failed, and then try again 1 millisecond later.   This is bad, and is like leaving a huge mess in the bedroom, but sometimes it is necessary because redesigning the table to have a sensible primary key would take too long. 
However, if you do this, be prepared for the code to be sitting in that while loop, trying and waiting patiently for tens of milliseconds, and when it finally does find an open slot, it doesn't do what you expect because it appears BEFORE another entry that occurred in the future (which was actually in the past).   Thinking about that makes my head hurt. 