I'm trying to query a MySQL DB with a date and return an id. My table is as follows:
| periodid | period |
| -------- | ---------- |
| 3 | 2014-04-01 |
| 7 | 2014-09-01 |
| 8 | 2014-07-01 |
| 9 | 2014-02-01 |
I can add records to this table using pymysql. But when I try to select the id with where clause, all I can get is 1. Here is the code:
import pymysql
from datetime import datetime
conn = pymysql.connect(user="", password="", host="", database="")
c = conn.cursor()
period = "01-02-2014"
date = datetime.strptime(period, '%d-%m-%Y')
# date returns datetime.datetime(2014, 2, 1, 0, 0)
c.execute("SELECT PeriodID FROM Periods WHERE Period = %s", (date, ))
# returns 1, always 1
I even tried as a string "2014-02-01" and also made a string out of date, using the code below:
str_date = date.strftime("%Y-%m-%d")
I still get 1 in return. After 1 hour of trying, I decided to ask. Thank you for your help.