【问题标题】:How to print a timestamp in a human readable format in SQLite3?如何在 SQLite3 中以人类可读格式打印时间戳?
【发布时间】:2011-03-25 10:47:21
【问题描述】:

我需要在 SQLite 中打印一个日期,比如“24-03-2011 10:45:00”,目前我只能打印日期。

>>> 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',)
>>>

有人可以告诉我如何做到这一点吗?

最好的问候,

【问题讨论】:

    标签: python sqlite timestamp


    【解决方案1】:

    date 只给你日期,因此得名。如果你想要日期和时间,你必须使用datetime

    如果您想格式化日期,您可以使用strftime - 在 SQL 命令或 Python 中。请参阅sqlite3 date docs

    这可行:

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

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 2020-07-02
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 2021-10-23
      • 2018-02-24
      相关资源
      最近更新 更多