【问题标题】:Formatting SQL query output in Python/SQLite 3在 Python/SQLite 3 中格式化 SQL 查询输出
【发布时间】:2010-07-20 11:12:15
【问题描述】:

我在 Python 中使用一个简单的 SQL 查询从 SQLite 3 数据库中获取记录:

cursor.execute ("SELECT due, task FROM tasks WHERE due <> '' ORDER BY due ASC")
        rows = cursor.fetchall()
            for row in rows:
                print '\n%s %s' % (row[0], row[1])

数据库中的due字段设置为DATE类型,所以查询返回该字段中的数据格式为2010-07-20 00:00:00.00如何删除 00:00:00.00,使结果只包含日期?谢谢!

【问题讨论】:

    标签: python sql sqlite


    【解决方案1】:

    我认为 row[0] 是一个 datetime 对象。所以以下应该工作:

    print '\n%s %s' % (row[0].strftime('%Y-%m-%d'), row[1])
    

    【讨论】:

    • 像魅力一样工作。 :-) 谢谢!
    • 你也可以像SELECT strftime('%Y-%m-%d', due), ...这样写查询
    猜你喜欢
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多