rjs
rjs

Reputation: 1

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").

I have looked through multiple stackoverflow questions and other online resources. In this case the FirstPacketTime db field is defined as an integer datatype (datetime in seconds). 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. 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.

Database schema

Query results - first 10 records

Code

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

Upvotes: 0

Views: 372

Answers (1)

roshan ok
roshan ok

Reputation: 383

A sample db created

enter image description here

enter image description here

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

Upvotes: 2

Related Questions