Python SQLITE substituted query returns nothing from database
<p>I am trying to execute the following query:</p>

<pre><code>sqlite&gt; select * from history where timeStamp &gt;= "2016-09-15 13:05:00" and timeStamp &lt; "2016-09-15 13:06:00";
timeStamp            isOpen    
-------------------  ----------
2016-09-15 13:05:04  0         
2016-09-15 13:05:09  0         
2016-09-15 13:05:14  1         
2016-09-15 13:05:19  1         
2016-09-15 13:05:24  1         
2016-09-15 13:05:29  1         
2016-09-15 13:05:34  1         
2016-09-15 13:05:39  0         
2016-09-15 13:05:44  1         
2016-09-15 13:05:49  1         
2016-09-15 13:05:54  1         
2016-09-15 13:05:59  0         
</code></pre>

<p>From Postman I run: <code>{{origin}}:{{port}}/logs?from=201609151305&amp;to=201609151306</code></p>

<p>In Python, I translate those values to: <code>2016/09/15 13:05:00</code> and <code>
2016/09/15 13:06:00</code>, which are passed to my helper method:</p>

<pre><code>vals = (lowStr, upperStr)
query = 'select * from history where timeStamp &gt;= ? and timeStamp &lt; ?'
returnList = accessDB('SELECT',query,vals)
</code></pre>

<p>AccessDB then does the following:</p>

<pre><code>def accessDB(operation, query, vals):
    con = None
    try:
        con = sqlite3.connect('logs.db')
        cur = con.cursor()
        cur.execute(query, vals)
        if operation == 'SELECT':
            return cur.fetchall()
        if operation == 'INSERT':
            con.commit()
    except sqlite3.Error as e:
        print("Error %s:" % e.args[0])
        sys.exit(1)
    finally:
        if con:
            con.close()
</code></pre>

<p>Nothing is being returned in the results, however. The return list is empty. What am I doing wrong?</p>

<p>In your example SQL, you are using datetimes with the format</p>

<pre><code>YYYY-MM-DD HH:MM:SS
</code></pre>

<p>In your Python example, however, you are using datetimes with the format</p>

<pre><code>YYYY/MM/DD HH:MM:SS
</code></pre>

<p>SQLite isn't seeing this as a valid date format. Change your <code>/</code>s to <code>-</code>s (how to do that depends on how you're doing the formatting).</p>

python sqlite select flask sqlite3