Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 通过asc的SQLite排序顺序_Python 2.7_Sqlite - Fatal编程技术网

Python 2.7 通过asc的SQLite排序顺序

Python 2.7 通过asc的SQLite排序顺序,python-2.7,sqlite,Python 2.7,Sqlite,我正在使用SQLite和python-2.7。在我的SQLite数据库中,我包含一个date字段,该字段以dd-MM-yyyy格式存储日期 31/02/2018 30/02/2017 01/06/2018 如何按升序排序。首先使用 connect = sqlite3.connect('datanase_file') cursor = connect.execute('select date from table_name') rows = cursor.fetchall() 然后将日期从历元

我正在使用
SQLite
python-2.7
。在我的
SQLite
数据库中,我包含一个
date
字段,该字段以
dd-MM-yyyy
格式存储日期

31/02/2018
30/02/2017
01/06/2018

如何按升序排序。

首先使用

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的转换时间列添加到数据库中

然后用

connect.execute('''UPDATE table_name SET converted_time = ? WHERE date = ?''',
               (converted_time, row[0]))

然后您可以使用任何程序对其进行排序。

首先使用

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的转换时间列添加到数据库中

然后用

connect.execute('''UPDATE table_name SET converted_time = ? WHERE date = ?''',
               (converted_time, row[0]))
然后,您可以使用任何程序对其进行排序。

尝试以下查询:

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
请尝试以下查询:

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

您不容易做到这一点,因为在SQLite中,所有日期都存储为纯文本。我建议将日期存储为
年/月/日
格式。然后,它们将根据您的需要进行排序。这里有一个解决办法,但这将是一个非常难看的查询,而且不值得你花很长时间。@macson taylor,你是在使用纯SQLite还是在使用其他语言?@Mr.Zeus我在使用python 2.7和SQLite。你不可能轻松做到这一点,因为在SQLite中,所有日期都存储为纯文本。我建议将日期存储为
年/月/日
格式。然后,它们将根据您的需要进行排序。这里有一个解决方法,但这将是一个非常难看的查询,而且不值得您花费太长时间。@macson taylor,您是在使用纯SQLite还是在使用其他语言?@Mr.Zeus我正在使用python 2.7和SQLite。