I have a sql statement that runs without error when I type it in to the sqlite3, but if I run it through my python script, it fails to recognize floor as a function.
I have a data set that contains the unix time (epoch), number of writes, and I want to capture the sum of the writes in various timed intervals. (10m, 20m, 1h, 2h etc). In this example, I am using 30m sample (1800 seconds). Here is the working SELECT Statement and the output when I run it sqlite3.exe
sqlite> SELECT "123456789" as ArraySN,
...> strftime('%Y-%m-%d %H:%M:%S', datetime(tsecs, 'unixepoch')) as "Date Time",
...> floor(tsecs/1800) * 1800 as "epoch",
...> 'W' as "R/W", sum(wsize5) as "16k"
...> FROM statvlun GROUP BY "Array SN", "epoch"
...> ORDER BY "epoch";
123456789|2021-03-03 05:20:00|1614747600|W|14410640.0
123456789|2021-03-03 05:30:00|1614749400|W|43205476.0
123456789|2021-03-03 06:00:00|1614751200|W|43193768.0
123456789|2021-03-03 06:30:00|1614753000|W|42348397.0
123456789|2021-03-03 07:00:00|1614754800|W|43197724.0
123456789|2021-03-03 07:30:00|1614756600|W|43196195.0
123456789|2021-03-03 08:00:00|1614758400|W|28802535.0
However, if I attempt to run this same query from python, it complains about the floor function not being recognized. I looked for special characters, tried single quotes, double quotes, triple quotes, encapsulating in the entire floor statement in parenthesis, and tried pandas vs sqlite3 module. Here is the sample code that I am running:
def main():
sql = """
SELECT "123456789" as ArraySN,
strftime('%Y-%m-%d %H:%M:%S', datetime(tsecs, 'unixepoch')) as \"Date Time\",
floor(tsecs/1800) * 1800 as "epoch",
'W' as "R/W", sum(wsize5) as "16k"
FROM statvlun GROUP BY "Array SN", "epoch"
ORDER BY \"epoch\";
"""
print(sql)
primdb_file = "testdb.db"
primdb_cnxn = sqlite3.connect(primdb_file)
cur = primdb_cnxn.cursor()
cur.execute(sql)
results = cur.fetchall()
print(results)
and I get sqlite3.OperationalError: no such function: floor
Here is the full output of the test script. What am I missing here?!?!
> python .\testsql.py
SELECT "123456789" as ArraySN,
strftime('%Y-%m-%d %H:%M:%S', datetime(tsecs, 'unixepoch')) as "Date Time",
floor(tsecs/1800) * 1800 as "epoch",
'W' as "R/W", sum(wsize5) as "16k"
FROM statvlun GROUP BY "Array SN", "epoch"
ORDER BY "epoch";
Traceback (most recent call last):
File ".\testsql.py", line 34, in <module>
main()
File ".\testsql.py", line 29, in main
cur.execute(sql)
sqlite3.OperationalError: no such function: floor