我正在使用SQLite
和python-2.7
。在我的SQLite
数据库中,我包含一个date
字段,该字段以这样的格式存储日期dd-MM-yyyy
。
31/02/2018
30/02/2017
01/06/2018
如何按升序排序。
尝试以下查询:
SELECT date(substr(`date_column`,7,4)||'-'||substr(`date_column`,4,2)||'-'||substr(`date_column`,1,2)) as text_date FROM `table` order by text_date asc
首先获取所有行
connect = sqlite3.connect('datanase_file')
cursor = connect.execute('select date from table_name')
rows = cursor.fetchall()
然后将日期从纪元转换为秒,以便对它们进行排序
for row in rows:
t = time.strptime(row[0],'%d/%m/%Y')
converted_time = time.mktime(t)
然后将类型为 INT converted_time列添加到数据库中。
然后更新您的数据库
connect.execute('''UPDATE table_name SET converted_time = ? WHERE date = ?''',
(converted_time, row[0]))
然后,您可以使用任何程序对其进行排序。