![]() |
Formatting date from MySQLdb
Hey all,
I've wrtting something that gathers the date from a mySQL database but I need to be able to grab the year, or month, or day, or everything if needed. When I get the date from my database I get this: (datetime.date(2007, 2, 9),) How would I format it so I can have the dates end up like this?: 2009-02-09 Any help is much appreciated! :banana: |
:
|
Cool, thanks.
Another question as I am fairly new to python. How do I apply the .strftime to that tuple that I posted? |
If you want to access an element of a tuple, you can use indices, in the same way you would an array or list:
:
:
:
:
|
Quote:
AttributeError: 'tuple' object has no attribute 'strftime' I can't figure out why this isn't working as it seems pretty straightfoward. :( |
What that means is that the object you're trying to reference is not a date object, but a tuple. Might I see your code?
|
I had to omit the SQL statement for work purposes, but here's all the code after that:
:
cursor.execute(sql_first_look) |
The "fetchall" function returns a list of tuples that represents the rows in your resultset. So result[0] returns the first item of the list; a tuple. You'd have to then index that again to get the first item of the tuple: result[0][0]
However, if you only expect your SQL to return one result, it's better to use "fetchone" instead. :
So it's likely you just need: :
|
That worked! Thanks a lot! :D :D :banana: :banana:
|
| All times are GMT -5. The time now is 1:58 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC