简体   繁体   中英

Searching a date in sqlite saved as TEXT - Performing greater, equal or less than operations

I am trying to retrieve a all the entities of my database with a date greater than a cerain given value.

I am using a sqlite database and thus the date ( record_date ) is expressed as TEXT .

My database is built in this way:

conn = sqlite3.connect('my_database.db')
    c = conn.cursor()
    c.execute("""
                INSERT INTO MY_TABLE (rating, record_date, url)
                     VALUES
                     (3, "2016-01-11", "www.site1.com"),
                     (3, "2016-01-04", "www.site2.com"),
                     (10, "2017-11-21", "www.site3.com"),
                     (0, "2017-04-30", "www.site4.com"),
                     (3, "2017-03-17", "www.site5.com"),
                     (1, "2016-03-29", "www.site6.com"),
                     (4.5, "2016-02-01", "www.site7.com"),
                     (3.2, "2016-02-12", "www.site8.com"),
                     (9, "2019-01-01", "www.site9.com"),
                     (6, "2017-10-04", "www.site10.com"),
                     (7, "2018-08-07", "www.site11.com"),
                     (8, "2018-06-07", "www.site12.com"),
                     (3, "2016-06-13", "www.site13.com"),
                     (2, "2017-09-02", "www.site14.com"),
                     (3, "2017-05-30", "www.site15.com")
              """)
    conn.commit()

My query written in python is the following:

db = sqlite3.connect(database_name)

cursor.execute("SELECT * FROM my_table WHERE strftime(record_date,'%Y-%m-%d') > ?",
(min_date_final,))
return self.cursor.fetchall()

min_date_final is a string expressed as year-month-day. Example: "2017-02-12"

I thought that strftime would be sufficient to transform the date TEXT into a date on which I can perform =, >, < operations but I am missing something.

You have date and format in wrong order

SELECT * 
FROM my_table 
WHERE strftime('%Y-%m-%d', record_date) > strftime('%Y-%m-%d', ?)

Try to use pandas and sqlite.


import pandas as pd
import sqlite3

conn = sqlite3.connect('my_database.db')
query = "SELECT * FROM my_table WHERE record_date > "2017-02-12;"

df = pd.read_sql_query(query,conn)

if your sqlite databank has a weird date format would skip the command where and load everything and filter it later in python.

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