简体   繁体   English

Sqlite 在 Python 中查询 integer 值字段不起作用

[英]Sqlite where query in Python on integer value field not working

I am trying to query a SQLite database for the rows that satisfy the condition cur.execute("SELECT * FROM data WHERE 'FirstPacketTime' > 1570680300") .我正在尝试查询 SQLite 数据库中满足条件cur.execute("SELECT * FROM data WHERE 'FirstPacketTime' > 1570680300")

I have looked through multiple stackoverflow questions and other online resources.我查看了多个 stackoverflow 问题和其他在线资源。 In this case the FirstPacketTime db field is defined as an integer datatype (datetime in seconds).在这种情况下,FirstPacketTime db 字段定义为 integer 数据类型(以秒为单位的日期时间)。 The row results are also coming back as integers, however the row data contains rows where the FirstPacketTime values are clearly less than the value shown above.行结果也以整数形式返回,但是行数据包含 FirstPacketTime 值明显小于上面显示的值的行。 Changing the greater than to an equal or less than ends up with no query results.将大于更改为等于或小于最终没有查询结果。 What am I missing here?我在这里想念什么? I have done queries before with python against MySQL databases with no issue.我之前使用 python 对 MySQL 数据库进行了查询,没有问题。

Database schema数据库架构

  • (0, 'FirstPacketTime', 'integer', 0, None, 0) (0,'FirstPacketTime','整数',0,无,0)
  • (1, 'SourceIP', 'text', 0, None, 0) (1, 'SourceIP', '文本', 0, 无, 0)
  • (2, 'SourcePort', 'integer', 0, None, 0) (2,'SourcePort','整数',0,无,0)
  • (3, 'DestinationIP', 'text', 0, None, 0) (3, 'DestinationIP', '文本', 0, 无, 0)
  • (4, 'DestinationPort', 'integer', 0, None, 0) (4, 'DestinationPort', '整数', 0, 无, 0)
  • (5, 'Protocol', 'text', 0, None, 0) (5, '协议', '文本', 0, 无, 0)
  • (6, 'TotalBytes', 'integer', 0, None, 0) (6, 'TotalBytes', '整数', 0, 无, 0)
  • (7, 'TotalPackets', 'integer', 0, None, 0) (7, 'TotalPackets', '整数', 0, 无, 0)

Query results - first 10 records查询结果 - 前 10 条记录

  • (1570676279, '19.116.151.212', 9876, '19.116.0.157', 53299, 'tcp_ip', 56, 1) (1570676279, '19.116.151.212', 9876, '19.116.0.157', 53299, 'tcp_ip', 56, 1)
  • (1570676279, '19.116.151.212', 9876, '19.116.0.157', 53301, 'tcp_ip', 56, 1) (1570676279, '19.116.151.212', 9876, '19.116.0.157', 53301, 'tcp_ip', 56, 1)
  • (1570650779, '19.116.1.36', 53497, '19.116.160.133', 102, 'tcp_ip', 67799, 696) (1570650779, '19.116.1.36', 53497, '19.116.160.133', 102, 'tcp_ip', 67799, 696)
  • (1570676339, '19.116.89.20', 3139, '19.116.29.147', 445, 'tcp_ip', 96, 2) (1570676339, '19.116.89.20', 3139, '19.116.29.147', 445, 'tcp_ip', 96, 2)
  • (1570676339, '19.116.89.20', 3479, '19.116.29.189', 445, 'tcp_ip', 96, 2) (1570676339, '19.116.89.20', 3479, '19.116.29.189', 445, 'tcp_ip', 96, 2)
  • (1570676339, '19.116.89.17', 3843, '19.116.29.33', 445, 'tcp_ip', 96, 2) (1570676339, '19.116.89.17', 3843, '19.116.29.33', 445, 'tcp_ip', 96, 2)
  • (1570676339, '19.116.89.24', 2398, '19.116.29.6', 445, 'tcp_ip', 96, 2) (1570676339, '19.116.89.24', 2398, '19.116.29.6', 445, 'tcp_ip', 96, 2)
  • (1570676339, '19.116.89.20', 3206, '19.116.29.159', 445, 'tcp_ip', 96, 2) (1570676339, '19.116.89.20', 3206, '19.116.29.159', 445, 'tcp_ip', 96, 2)
  • (1570676339, '19.116.89.20', 3161, '19.116.29.151', 445, 'tcp_ip', 96, 2) (1570676339, '19.116.89.20', 3161, '19.116.29.151', 445, 'tcp_ip', 96, 2)
  • (1570676339, '19.116.89.15', 1082, '19.116.0.16', 445, 'tcp_ip', 96, 2) (1570676339, '19.116.89.15', 1082, '19.116.0.16', 445, 'tcp_ip', 96, 2)

Code代码

cur.execute("SELECT * FROM data WHERE 'FirstPacketTime' > 1570680300")
rows = cur.fetchmany(10)

A sample db created创建了一个示例数据库

在此处输入图像描述

在此处输入图像描述

Below query was executed, and was able to get the records执行了以下查询,并且能够获取记录

import sqlite3
conn = sqlite3.connect('test.db')
c = conn.cursor()
print (c.execute("SELECT * FROM test WHERE FirstPacketTime < 1570676280").fetchall())
print (c.execute("SELECT * FROM test WHERE FirstPacketTime = 1570676279").fetchall())
print (c.execute("SELECT * FROM test WHERE FirstPacketTime = 1570676339").fetchall())

[(1570676279, '19.116.151.212', 9876)]
[(1570676279, '19.116.151.212', 9876)]
[(1570676339, '19.116.89.20', 3139)]

What i did is Removed the single quotes.我所做的是删除了单引号。

Also, initially it didnt appear for me, because i had not committed the data to database.此外,最初它并没有出现在我身上,因为我没有将数据提交到数据库。

Once the data was commited to the database, i was able to get the output一旦数据被提交到数据库,我就可以得到 output

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

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