Click here to Skip to main content
12,815,616 members (40,353 online)

Use mysql DATE_FORMAT in Python

Madzmar25 asked:

Open original thread
Hello guys.. I was wondering if any of you tried using DATE_FORMAT in python? you see I'm trying to format one of my field using date_format but the problem is when I try to run it in python it gives an error since % symbol is reserved char for python here is my python code
def get_announcement(con):
        c = con.cursor(DictCursor)
        query = """
                    DATE_FORMAT(start_date, '%M %D, %Y'),
                    DATE_FORMAT(end_date, '%M %D, %Y')
                FROM announcement
                ORDER BY id DESC;                                    
        dbres = c.fetchall()
        if not dbres: return None
        return dbres
        if c: c.close()

But the problem is i cant get this to work since python is complaining about the formatting of the start_date and end_date do you have any idead on how to solve this in a query way. As much as possible I dont want to let python do the date formatting for me instead I want to retrieve the data that has been formatted already in mysql

Please give me your idead and answers thanks and more power
Tags: Python (Python2.3, Python2.4, Python2.7)


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100