Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 12th, 2007, 3:06 PM   #1
poopman
Newbie
 
Join Date: Feb 2007
Posts: 13
Rep Power: 0 poopman is on a distinguished road
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:
poopman is offline   Reply With Quote
Old Feb 12th, 2007, 3:12 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
python Syntax (Toggle Plain Text)
  1. dateObject.strftime("%Y-%m-%d")
Arevos is offline   Reply With Quote
Old Feb 12th, 2007, 3:21 PM   #3
poopman
Newbie
 
Join Date: Feb 2007
Posts: 13
Rep Power: 0 poopman is on a distinguished road
Cool, thanks.

Another question as I am fairly new to python. How do I apply the .strftime to that tuple that I posted?
poopman is offline   Reply With Quote
Old Feb 12th, 2007, 5:04 PM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
If you want to access an element of a tuple, you can use indices, in the same way you would an array or list:
python Syntax (Toggle Plain Text)
  1. result = (datetime.date(2007, 2, 9),)
  2. date = result[0]
  3. print date.strftime("%Y-%m-%d")
Or simply:
python Syntax (Toggle Plain Text)
  1. print result[0].strftime("%Y-%m-%d")
You can also assign a tuple to an set of variables to get its contents:
python Syntax (Toggle Plain Text)
  1. coords = (4, 6)
  2. x, y = coords
And for a tuple with a single value:
python Syntax (Toggle Plain Text)
  1. result = (datetime.date(2007, 2, 9),)
  2. date, = result
This notation when applied to only one value is a little obscure, however.
Arevos is offline   Reply With Quote
Old Feb 12th, 2007, 5:22 PM   #5
poopman
Newbie
 
Join Date: Feb 2007
Posts: 13
Rep Power: 0 poopman is on a distinguished road
Quote:
Originally Posted by Arevos View Post
If you want to access an element of a tuple, you can use indices, in the same way you would an array or list:
python Syntax (Toggle Plain Text)
  1. result = (datetime.date(2007, 2, 9),)
  2. date = result[0]
  3. print date.strftime("%Y-%m-%d")
Or simply:
python Syntax (Toggle Plain Text)
  1. print result[0].strftime("%Y-%m-%d")
You can also assign a tuple to an set of variables to get its contents:
python Syntax (Toggle Plain Text)
  1. coords = (4, 6)
  2. x, y = coords
And for a tuple with a single value:
python Syntax (Toggle Plain Text)
  1. result = (datetime.date(2007, 2, 9),)
  2. date, = result
This notation when applied to only one value is a little obscure, however.
Thanks for this. I tried out all these methods and kept coming up with this error:

AttributeError: 'tuple' object has no attribute 'strftime'

I can't figure out why this isn't working as it seems pretty straightfoward.
poopman is offline   Reply With Quote
Old Feb 12th, 2007, 5:38 PM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
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?
Arevos is offline   Reply With Quote
Old Feb 12th, 2007, 5:45 PM   #7
poopman
Newbie
 
Join Date: Feb 2007
Posts: 13
Rep Power: 0 poopman is on a distinguished road
I had to omit the SQL statement for work purposes, but here's all the code after that:
cursor.execute(sql_first_look)
result = cursor.fetchall()
date = result[0]

print date.strftime("%Y-%m-%d")
connection.commit()
cursor.close()
connection.close()
poopman is offline   Reply With Quote
Old Feb 12th, 2007, 5:57 PM   #8
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
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.
python Syntax (Toggle Plain Text)
  1. cursor.execute(sql_first_look)
  2. result = cursor.fetchone()
  3. date = result[0]
  4.  
  5. print date.strftime("%Y-%m-%d")
Also, I'm not sure you need connection.commit() unless your SQL statement is part of a transaction that alters the database. Nor do you need to close your cursor separately to your database; closing the database closes the cursor too.

So it's likely you just need:
python Syntax (Toggle Plain Text)
  1. connection.close()
Arevos is offline   Reply With Quote
Old Feb 12th, 2007, 6:01 PM   #9
poopman
Newbie
 
Join Date: Feb 2007
Posts: 13
Rep Power: 0 poopman is on a distinguished road
That worked! Thanks a lot! :banana: :banana:
poopman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Routine tbohon Perl 2 Jan 15th, 2007 11:14 AM
converting string to float beginnerCCC C 22 Oct 2nd, 2006 11:59 PM
Php Postgresql Class Pizentios Show Off Your Open Source Projects 15 Jun 28th, 2005 9:55 AM
How custom formatting for date picker is achieved? sham Visual Basic 1 Apr 9th, 2005 5:56 PM
Enquiry About Visual Basic Project kbkhoo5053 Visual Basic 13 Feb 15th, 2005 2:20 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:17 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC