在Python中使用mysql DATE_FORMAT [英] Use mysql DATE_FORMAT in Python

查看:762
本文介绍了在Python中使用mysql DATE_FORMAT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..我想知道你们有没有尝试在python中使用DATE_FORMAT?你看我正在尝试使用date_format格式化我的一个字段,但问题是当我尝试在python中运行它时会出现错误,因为%符号是python的保留字符这里是我的python代码

 def get_announcement(con):
try:
c = con.cursor(DictCursor)
query =
SELECT
id ,
page_id,
title,
内容,
bnr_img,
DATE_FORMAT(start_date,''%M%D,%Y''),
DATE_FORMAT(end_date,''%M%D,%Y'')
FROM公告
ORDER BY id DESC;

c.execute(query)
dbres = c.fetchall()
如果不是dbres:return无
返回dbres
finally:
如果c:c.close()



但是问题是我不能让这个工作,因为python抱怨start_date和end_date的格式你是否有任何idead如何以查询方式解决这个问题。尽可能我不想让python为我做日期格式化而我想要检索已经在mysql中格式化的数据



请给我你的理想和答案谢谢和更多的力量

解决方案

别介意我已经解决了它...只是为了记录这里我想出的是

 def get_announcement(con):
try:
c = con.cursor(DictCursor)
query =
SELECT
id,
page_id,
title,
content,
bnr_img,
DATE_FORMAT(start_date,%s),
DATE_FORMAT(end_date,%s )
FROM公告
ORDER BY id DESC;

format =''%M%D,%Y''
c.ex ecute(query,[format,format])
dbres = c.fetchall()
如果不是dbres:return无
返回dbres
finally:
如果c: c.close()


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):
    try:
        c = con.cursor(DictCursor)
        query = """
                SELECT 
                    id,
                    page_id,
                    title,
                    contents,
                    bnr_img,
                    DATE_FORMAT(start_date, ''%M %D, %Y''),
                    DATE_FORMAT(end_date, ''%M %D, %Y'')
                FROM announcement
                ORDER BY id DESC;                                    
                """
        c.execute(query)
        dbres = c.fetchall()
        if not dbres: return None
        return dbres
    finally:
        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

解决方案

Never mind guys I already solved it... Just for the record here''s what I came up with

def get_announcement(con):
    try:
        c = con.cursor(DictCursor)
        query = """
                SELECT 
                    id,
                    page_id,
                    title,
                    contents,
                    bnr_img,
                    DATE_FORMAT(start_date, %s),
                    DATE_FORMAT(end_date, %s)
                FROM announcement
                ORDER BY id DESC;                                    
                """
        format= "''%M %D, %Y''"
        c.execute(query,[format,format])
        dbres = c.fetchall()
        if not dbres: return None
        return dbres
    finally:
        if c: c.close()


这篇关于在Python中使用mysql DATE_FORMAT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆