简体   繁体   中英

How to print a timestamp in a human readable format in SQLite3?

I need to print a date in SQLite like this "24-03-2011 10:45:00", for now I only can print the date.

>>> import sqlite3
>>> database = sqlite3.connect('basedadosteste.db')
>>> cursor = database.cursor()
>>> cursor.execute("SELECT date('now');")
<sqlite3.Cursor object at 0x00BC0710>
>>> results = cursor.fetchall()
>>> for entry in results:
...     print entry
...
(u'2011-03-25',)
>>>

Can someone give me a clue on how to do this?

Best Regards,

date only gives you the date, hence the name. If you want the date and time, you have to use datetime .

And if you want to format the date, you can use strftime - either in the SQL command, or in Python. See the sqlite3 date docs .

This would work:

SELECT strftime('%d-%m-%Y %H:%M:%S', 'now');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM