如何使用python在sqlite数据库中按年份搜索日期?

如果我们要按年份和格式(例如年-月-日期)从表中搜索列或记录 那怎么找。我有一个表,该表具有与天气有关的23列,我想访问所有数据2010,将使用哪个查询,请帮助任何人..thankx 我会尝试,但没有任何输出:

c.execute(“ select * from lahore_weather_2009_May where substr(PKT,1,4)='2011'”)

如果我按月将这段代码应用于访问,那么也不会给出任何输出:

validmonth =假         无效时:             lookForMonth = input('请问是几月?(从1到12的数字):')             尝试:                 validmonth = 1

            sqlCmd = 'SELECT PKT FROM lahore_weather_2009_May WHERE SUBSTR(PKT,4,2)="%.2i"' % int(lookForMonth)
            for row in conn.execute(sqlCmd):
                print (row)
                c.commit()

已在数据库中创建的表

icoast 回答:如何使用python在sqlite数据库中按年份搜索日期?

SQLite没有DATE数据类型,因此日期存储为TEXT
假定日期列的格式为YYYY-MM-DD,则可以使用函数strftime()

select * from tablename
where strftime('%Y',datecolumn) = '2010'

模式'%Y'提取日期的年份。
或使用substr()获取从日期开始的4个字符:

select * from tablename
where substr(datecolumn,1,4) = '2010'
本文链接:https://www.f2er.com/3115198.html

大家都在问