0

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.

1 Answer 1

1

After executing the cursor.execute you have to fetch the records

Try adding fetchall() at the end and check

For more information check below link

https://thepythonguru.com/fetching-records-using-fetchone-and-fetchmany/

Sign up to request clarification or add additional context in comments.

1 Comment

That's what I needed. Thanks a lot :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.