3

I have a datetime object that I want to INSERT in an SQL database TIMESTAMP column.

However, the procedure always fails due to formatting mismatch.

The datetime object:

date = datetime.datetime.strptime('2021-07-21 00:00:00', '%Y-%m-%d %H:%M:%S')

I tried to format it this way:

datetime.datetime.timestamp(date)

But it didn't work out, I think because the timestamp() function returns a float.

Is there a way to properly convert the datetime object to timestamp?

5
  • 1
    This has already been answered Commented Jul 20, 2021 at 13:56
  • not really. This one converts to datetime, but I want to convert datetime to timestamp Commented Jul 20, 2021 at 14:01
  • Which is your target DBMS? Commented Jul 20, 2021 at 14:52
  • its mysql database Commented Jul 20, 2021 at 16:19
  • The top answer in the linked question above doesn't convert to datetime, it converts to string. .strftime('%Y-%m-%d %H:%M:%S') this has worked for me uploading to timestamp fields in several types of SQL (admittedly, never tried it in mysql) Commented Aug 5, 2021 at 0:53

0

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.