如何使用python在sqlite数据库中按年搜索日期?
若我们想从表中按年份和格式(如年-月-日期)搜索列或记录,那个么如何检索。我有一个表,其中有23列与天气有关,我想访问查询将使用的所有2010年数据 我将尝试它,但它没有输出:如何使用python在sqlite数据库中按年搜索日期?,python,python-3.x,sqlite,Python,Python 3.x,Sqlite,若我们想从表中按年份和格式(如年-月-日期)搜索列或记录,那个么如何检索。我有一个表,其中有23列与天气有关,我想访问查询将使用的所有2010年数据 我将尝试它,但它没有输出: c.execute("select * from lahore_weather_2009_May where substr(PKT, 1, 4) = '2011'") validMonth = False while not validMonth: lookForMonth = input('What mont
c.execute("select * from lahore_weather_2009_May where substr(PKT, 1, 4) = '2011'")
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
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()
如果我按月将此代码应用于Access,则也不会给出任何输出:
c.execute("select * from lahore_weather_2009_May where substr(PKT, 1, 4) = '2011'")
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
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()
validMonth=False
虽然不是有效月份:
LookFormMonth=input('请问是哪一个月?(从1到12的数字):')
尝试:
validMonth=1SQLite没有日期
数据类型,因此日期存储为文本
假设日期列的格式为YYYY-MM-DD
,则可以使用以下函数:
模式'%Y'
提取日期的年份。
或用于从日期开始获取4个字符:
select * from tablename
where substr(datecolumn, 1, 4) = '2010'
请提供一个你到目前为止尝试过的例子,以及为什么它不起作用。我不会按年份尝试,我会按月份尝试,像这样如果日期的格式是YYYY-MM-DD,那么SUBSTR(PKT,4,2)将不会返回月份。使用SUBSTR(PKT,6,2)。