DateTime上的SQLite查询未返回任何结果,或返回所有结果
我已经研究了这个主题为2天,我认为这是一个简单的问题,但无法产生任何返回我想要的结果的结果。
我目前有一个带有以下模式的数据库,我正在尝试查询以从一定时间段内提取结果。
sqlite> .schema
CREATE TABLE tbLogger(time INT, temp REAL, fanOn INT);
从选择 *显示返回的示例以下显示:
2022-05-26 03:51:53.832585-04:00 78.8 1
2022-05-26 03:52:57.410400-04:00 78.8 1
2022-05-26 03:54:04.549302-04:00 78.8 1
2022-05-26 03:55:08.179198-04:00 78.8 1
2022-05-26 03:56:11.808473-04:00 78.8 1
2022-05-26 03:57:15.447874-04:00 78.8 1
2022-05-26 03:58:22.605812-04:00 78.8 1
2022-05-26 03:59:26.219187-04:00 78.8 1
如果我正确理解的话,我将以时代的时间格式存储数据,我相信我相信我将新数据插入到记录程序中的方式( python):
with sqlite3.connect(db_filename) as conn:
conn.execute("insert into tbLogger values(?,?,?)",
(datetime.datetime.now(tz = timezone), temp_f, fanOn))
当我尝试在我知道存在的特定时间查询时,查询不会返回
sqlite> SELECT * FROM tbLogger WHERE time = datetime(1653465566, 'unixepoch');
sqlite>
结果
SELECT * FROM tbLogger WHERE time BETWEEN datetime(1653364800, 'unixepoch') AND datetime(1653451140, 'unixepoch');
2022-05-25 09:46:54.551584-04:00 78.8 1
2022-05-25 09:47:58.275281-04:00 78.8 1
2022-05-25 09:49:08.986005-04:00 78.8 1
..... continued
任何 在查询中,我在Where语句中符合“之间”的标准。我的一生,无法弄清楚我实际上需要在陈述中获取我想要的信息的实际价值。我希望这是一个简单的错误,我错过了一些如此简单的事情,以至于我飞过它。
本质上,我正在寻找一种在两个日期之间查询的方法(无论时间如何),并从数据库中返回所有这些结果,以拉入我的绘图仪。
如果有一种更简单的方法将数据存储到数据库中,那么我将非常简单地开始新鲜,因此如果这是首选的解决方案,则不会出现问题。在我阅读许多堆栈帖子时,INT似乎是在SQLite中存储时间的最佳选择,因此我希望至少那部分是正确的!
预先感谢您的帮助。
因此,尽管我将数据存储为int
,但似乎仍将数据存储为string
。当我查询时: 从tblogger中选择 *,例如'2022-05-25 22:%';
我被返回了我要寻找的结果:
2022-05-25 22:00:27.495144-04:00|80.6|1
2022-05-25 22:01:31.201165-04:00|80.6|1
2022-05-25 22:02:34.906498-04:00|80.6|1
2022-05-25 22:03:38.582368-04:00|80.6|1
2022-05-25 22:04:45.744486-04:00|80.6|1
我想这回答了我如何查询我的最终问题,现在,我可以在服务器上有时间和日期都有字段,并使用类似语句,但这并不能解决为什么int
像String
一样存储。
I have been researching this topic for 2 days on what I thought would be a simple question, but have not been able to yield any results that return what I am looking for.
I currently have a database with the following schema, that I am trying to query to pull results from a certain time period.
sqlite> .schema
CREATE TABLE tbLogger(time INT, temp REAL, fanOn INT);
A sample of the return from a select * displays the following:
2022-05-26 03:51:53.832585-04:00 78.8 1
2022-05-26 03:52:57.410400-04:00 78.8 1
2022-05-26 03:54:04.549302-04:00 78.8 1
2022-05-26 03:55:08.179198-04:00 78.8 1
2022-05-26 03:56:11.808473-04:00 78.8 1
2022-05-26 03:57:15.447874-04:00 78.8 1
2022-05-26 03:58:22.605812-04:00 78.8 1
2022-05-26 03:59:26.219187-04:00 78.8 1
If I am understanding correctly, I am storing the data in epoch time format, which I beleive is confirmed by the way that I am inserting the new data in from the logging program (Python):
with sqlite3.connect(db_filename) as conn:
conn.execute("insert into tbLogger values(?,?,?)",
(datetime.datetime.now(tz = timezone), temp_f, fanOn))
When I try to query against a specific time that I know exists, the query returns no results
sqlite> SELECT * FROM tbLogger WHERE time = datetime(1653465566, 'unixepoch');
sqlite>
However if I query with BETWEEN two time periods, then all results in the database are returned:
SELECT * FROM tbLogger WHERE time BETWEEN datetime(1653364800, 'unixepoch') AND datetime(1653451140, 'unixepoch');
2022-05-25 09:46:54.551584-04:00 78.8 1
2022-05-25 09:47:58.275281-04:00 78.8 1
2022-05-25 09:49:08.986005-04:00 78.8 1
..... continued
This is leading me to believe that when I am querying, I am meeting the criteria of "between" with the conversion to datetime in the WHERE statement. I for the life of me, cannot figure out what values I am actually needing to place in the WHERE statement to get the information I am looking for. I am hoping that this is a simple error and I am missing something so easy that I am flying over it.
Essentially, I am looking for a way to query between two dates (regardless of the times) and return all of those results from the db, to pull into my plotter.
If there is an easier way to store the data into the database, it will be very simple for me to start fresh, so no problems if that is the preferred solution. In my reading of many stack posts, INT seemed to be the best to store times in SQLite, so I hope at least that part was right!
Thank you in advance for the help.
So it would seem that despite the fact that I am storing the data as INT
, the data is being stored as a STRING
. When I query:SELECT * FROM tbLogger WHERE time LIKE '2022-05-25 22:%';
I am returned the results that I am looking for:
2022-05-25 22:00:27.495144-04:00|80.6|1
2022-05-25 22:01:31.201165-04:00|80.6|1
2022-05-25 22:02:34.906498-04:00|80.6|1
2022-05-25 22:03:38.582368-04:00|80.6|1
2022-05-25 22:04:45.744486-04:00|80.6|1
I guess this answers my ultimate question of how I can query, as I now can have fields on the server for both time and date and use the like statement, but that does not solve why the INT
is being stored like a STRING
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
还添加到主文章中:
因此,尽管我将数据存储为
int
,但似乎仍将数据存储为string
。当我查询时:从tblogger中选择 *,例如'2022-05-25 22:%';
我返回了我要寻找的结果:
我想这是我如何回答我如何回答了我如何回答的。可以查询,因为我现在可以在时间和日期都使用服务器上的字段并使用类似语句,但这并不能解决为什么int像字符串一样存储的原因。
Added to main post as well:
So it would seem that despite the fact that I am storing the data as
INT
, the data is being stored as aSTRING
. When I query:SELECT * FROM tbLogger WHERE time LIKE '2022-05-25 22:%';
I am returned the results that I am looking for:
I guess this answers my ultimate question of how I can query, as I now can have fields on the server for both time and date and use the like statement, but that does not solve why the INT is being stored like a STRING.