sql - python: query a DATETIME field in a sqlite db for a date range -
have sqlite db i've created in python has datetime field:
import sqlite3 con = sqlite3.connect('some.db',detect_types=sqlite3.parse_decltypes) con: cur = con.cursor() cur.execute("create table table(...date datetime...)") ... date = datetime.datetime(<a format resolves correct datetime object>) ... altogether = (..., date, ...) cur.execute("insert table values(...?...)", altogether) con.commit() this populates correctly. later want able query db datetime, , have function manage queries generally:
def query_db(path, query, args=(), one=false): connection = sqlite3.connect(path) cur = connection.execute(query, args) rv = [dict((cur.description[idx][0], value) idx, value in enumerate(row)) row in cur.fetchall()] return (rv[0] if rv else none) if 1 else rv local_folder = os.getcwd() samplequery = "select * table" dbfile = os.path.join(local_folder, "some.db") result = query_db(dbfile, samplequery) the above produce resultthat gave me in table.
however, how structure query would, instance, give me entries in table table of some.db have date within past 60 days?
you can query this:
select * table date >= date('now', '-60 day'); edit:
based on actual query:
select <field1>, <field2>, count(1) num table date >= date('now', '-60 day'); group <field1>, <field2>; select distinct unnecessary when using group by.
Comments
Post a Comment