0

I'm trying to run a pymysql select statement, and use the datetime in my code. I was able to print the output and the query is correct, but I can't run dict commands on the variable I used to store the data. It keeps saying the type of the object is None, even though I declared it as an empty dict at the start of my function.

The output return as follows: {'last_date': datetime.datetime(2023, 4, 9, 13, 30)}. How do I manipulate these numbers so I can build a datetime objetct in my script?

EDIT: my query is: "SELECT last_date from mysqldb.table_name ORDER BY last_date DESC LIMIT 1"

last_date type in the database is datetime(3)

EDIT2: Here is the code:

last_date = {}
sqlQuery = "(...)"
cursorObject.execute(sqlQuery)
last_date = cursorObject.fetchone()

I'm having trouble to access the data inside last_date, when using dict functions it returns like: (...) is not a known member of "None"

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You haven't shown your query. That's pretty important. – roganjosh Apr 10 '23 at 16:19
  • 1
    Please post your Python code. The dictionary you show is clearly not `None`, so we need to see where you're getting that. And include the full traceback. – Barmar Apr 10 '23 at 16:23
  • `last_date` will be `None` if the query doesn't return anything. But since the query has no `WHERE` condition, that would only happen if the table is empty. – Barmar Apr 10 '23 at 16:41
  • That error message doesn't come from Python, it comes from Pylance. Your code should be using `last_date['last_date']` to access the dictionary element. – Barmar Apr 10 '23 at 16:43
  • See https://stackoverflow.com/questions/68446642/how-do-i-get-pylance-to-ignore-the-possibility-of-none for how to fix the Pylance warning. – Barmar Apr 10 '23 at 16:44

0 Answers0