简体   繁体   English

sqlite3 datetime.datetime python选择

[英]sqlite3 datetime.datetime python select

The sqlite3 database is hundreds of thousands of rows. sqlite3数据库有成千上万的行。 I want to sort it by the column with (datetime.datetime(now)) 我想按(datetime.datetime(now))对列进行排序

The table might be: 该表可能是:

c.execute(“CREATE TABLE IF NOT EXISTS table_one (time_column TEXT,column_two REAL,column_three REAL)”)

The time column is TEXT time栏为TEXT

The inserts have worked fine and I have hundreds of thousands of rows. 插入效果很好,我有成千上万的行。 I need to look at the data by time. 我需要按时间查看数据。 For example “what is the average and standard deviation on January 3, 2019 between 1:00am and 2:00am” (this would be about 300 numbers) 例如,“ 2019年1月3日凌晨1:00和凌晨2:00之间的平均偏差和标准偏差是多少”(大约300个数字)

Something like this: 像这样:

all_rows =  c.execute('SELECT  column_two   FROM table_one WHERE time_column >  2019-01-03  01:00:00:000000  and time_column <  2019-01-03  02:00:00:000000  ‘)

The fetchall needs to go into an array for statistical analysis, but that is another problem. fetchall需要进入数组进行统计分析,但这是另一个问题。

At present the retrieve of the datetime is fine. 目前,可以检索datetime时间。 It comes across as a unicode (whatever that is) 它以unicode (无论是unicode形式)

But I need to make decisions with the datetime field. 但是我需要使用datetime字段进行决策。 Is the TEXT a problem? TEXT有问题吗? I could add a column and change the date stamp to a 'unix time stamp' which appears to be a strait number. 我可以添加一列,然后将date stamp更改为“ unix时间戳”,这似乎是一个海峡数字。

I am learning more and more, but stuck on this …. 我正在学习越来越多的东西,但是坚持下去……。 help (and thanks) 帮助(谢谢)

You are essentially asking for advices on how to design a database to improve efficiencies of some requests. 您实质上是在寻求有关如何设计数据库以提高某些请求效率的建议。 That could be rather broad but I will try for this simple example. 可能范围很广,但是我将尝试这个简单的例子。

First (and for your question), the TEXT type for the time column is acceptable for SQLite which has no dedicated Date type. 首先(也是您的问题),对于没有专用日期类型的SQLite,时间列的TEXT类型是可接受的。 The date are converted in ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS"), which ensures compatibility with the comparisons operators. 日期将转换为ISO8601字符串(“ YYYY-MM-DD HH:MM:SS.SSS”),以确保与比较运算符兼容。 The good questions to ask is what is the range for your date values, and whether you need to evaluate date differences. 要问的好问题是日期值的范围是多少,以及是否需要评估日期差。 If you only need a one second precision with dates greater that 1970-01-01, then INTEGER (number of seconds since 1970-01-01 00:00:00 UTC) is good, and REAL (Julian day numbers) is good if exact precision does not matter (because of floating point inaccuracy), but a large range is required. 如果您只需要一秒的精度且日期大于1970-01-01,则INTEGER(自1970-01-01 00:00:00 UTC以来的秒数)很好,而REAL(Julian天数)如果满足精确的精度无关紧要(因为浮点数不准确),但是需要很大的范围。

But what matters most for query efficiency is the presence of an index. 但是,对于查询效率而言最重要的是索引的存在。 So if you need to improve queries using the time_column , declare an index on it. 因此,如果您需要使用time_column改进查询,请在其上声明一个索引。 Your code could become: 您的代码可能变为:

c.execute("CREATE TABLE IF NOT EXISTS table_one (time_column TEXT,column_two REAL,"
           "column_three REAL)")
c.execute("CREATE INDEX IF NOT EXISTS index_time_table_one ON table_one(time_column)")

and the select part would be: 选择部分将是:

curs = c.execute("SELECT  column_two   FROM table_one WHERE time_column"
                 "BETWEEN '2019-01-03 01:00:00'  and '2019-01-03 02:00:00'")

Simply that way, you will get an ISO string representations when you fetch the time_column column. 这样一来,在获取time_column列时,您将获得ISO字符串表示形式。

Python SQLite3 module is even smarter with dates, and is able to automatically convert them to timestamp, provided you kindly ask it: Python SQLite3模块甚至可以更智能地显示日期,并且可以自动将它们转换为时间戳,只要您提出以下要求即可:

# declare that you want to use custom datatypes, declare in columns
c = sqlite3.connect('your_db', detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
# declare the time_column to be a timestamp:
c.execute("CREATE TABLE IF NOT EXISTS table_one (time_column timestamp,column_two REAL,"
           "column_three REAL)")
# declare the index
c.execute("CREATE INDEX IF NOT EXISTS index_time_table_one ON table_one(time_column)")

When you database is populated you can fetch it as usual and retrieve directly Python datetime object: 填充数据库后,您可以照常获取它并直接检索Python datetime对象:

curs = c.execute("SELECT * FROM table_one WHERE time_column BETWEEN"
                 " '2019-01-03 01:00:00'  and '2019-01-03 02:00:00'")
r = curs.fetchone()
print(r)

will output: 将输出:

(datetime.datetime(2019, 1, 2, 0, 0), ...)

The drawback is that is is no longer standard SQL and will require tweaking if you use a different database. 缺点是不再是标准SQL,如果您使用其他数据库,则需要进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM