简体   繁体   中英

Python3 SQLite query

I am having an issue with a Python3 sqlite query.

This is the following query I ran from the sqlite3 interpreter, it works perfectly:

  SELECT {some_columns} FROM {tableA} WHERE bus='ABCD' AND 
    datetime(date_column||" "||time_column) >= datetime('2019-03-19 05:30:00') 
    INTERSECT
  SELECT {some_columns} FROM {tableA} WHERE bus='ABCD' AND
    datetime(date_column||" "||time_column) <= datetime('2019-03-19 05:30:00', '+135 minutes');

However when I run the same query in Python as below, it always returns None

cur.execute('''SELECT colA FROM tableA WHERE bus='ABCD' AND
    datetime(date_column||" "||time_column) >= dateteime(?) 
    INTERSECT
    SELECT colA FROM tableA WHERE bus='ABCD' AND
    datetime(date_column||" "||time_column) <= dateteime(?)''', (
bus,
dt.strftime('%Y-%m-%d %H:%M:%S'),
"'%s', '+%s minutes'" % (lookup_time.strftime("%Y-%m-%d %H:%M:%S"),
                                                              duration)

)).fetchall()

Any sort of help or ideas would be appreciated. The 'bus' and date/time columns have been properly defined in the SQlite tables.

SOLVED IT!

I used the datetime module instead. My final python code was:

lookup_end_time = lookup_time + datetime.timedelta(minutes=135)

...code trimmed here...

cur.execute('''SELECT colA FROM tableA WHERE bus='ABCD' AND
    datetime(date_column||" "||time_column) >= dateteime(?) 
    INTERSECT
    SELECT colA FROM tableA WHERE bus='ABCD' AND
    datetime(date_column||" "||time_column) <= dateteime(?)''', (
bus,
dt.strftime('%Y-%m-%d %H:%M:%S'),
lookup_end_time

)).fetchall()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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