pymysql和sqlalchemy:AttributeError:'DateTime'对象没有属性'translate'

时间:2018-04-28 20:34:12

标签: mysql datetime sqlalchemy

在尝试将新记录保存到数据库时,我收到上述错误,与我拥有的DateTime属性(engagementDate)有关。

我传递给sqlalchemy的值的类型看起来没问题:

<wx.DateTime: "Sat Apr 28 00:00:00 2018">

在MySql上,我的架构具有以下设置:

默认排序规则:utf8_general_ci

默认字符集:utf8

日期值来自wx.Dialog(表单)上的wx.adv.DatePickerCtrl,在我的本地utf8中正确显示和验证。

我的MySQL连接字符串已定义本地:utf8mb4。

该表是使用此模型创建的:

class Contractors(DeclarativeBase):
    __tablename__ = "contractors"

    id = Column(Integer, primary_key=True)
    company_name = Column("company_name", Unicode(80))
    company_address = Column("company_address", String(250))
    ...
    is_training_provider = Column("is_training_provider", Boolean())
    engagement_date = Column("engagement_date", DateTime)
    comments = Column("comments", Text())

我的模特:

class OlvContractors(object):

    def __init__(self, id, company_name, company_address, company_city, company_zip, company_country, company_phone1,
                 company_phone2, company_description, contact_person, cp_email, cp_phone1, cp_phone2,
                 is_training_provider, engagement_date, comments):
        self.id = id  # unique row id from database
        self.company_name = company_name
        self.company_address = company_address
        ...
        self.is_training_provider = is_training_provider
        self.engagement_date = engagement_date
        self.comments = comments

sqlalchemy生成的SQL语句是:

INSERT
INTO
contractors(company_name, company_address, company_city, company_zip, company_country, company_phone1, company_phone2,
            company_description, contact_person, cp_email, cp_phone1, cp_phone2, is_training_provider, engagement_date,
            comments)
VALUES( % (company_name)
s, % (company_address)
s, % (company_city)
s, % (company_zip)
s, % (company_country)
s, % (company_phone1)
s, % (company_phone2)
s, % (company_description)
s, % (contact_person)
s, % (cp_email)
s, % (cp_phone1)
s, % (cp_phone2)
s, % (is_training_provider)
s, % (engagement_date)
s, % (comments)
s)
{'company_name': '', 'company_address': '', 'company_city': '', 'company_zip': '', 'company_country': '',
 'company_phone1': '', 'company_phone2': '', 'company_description': '', 'contact_person': '', 'cp_email': '',
 'cp_phone1': '', 'cp_phone2': '', 'is_training_provider': 0, 'engagement_date'
 : < wx.DateTime: "Sat Apr 28 00:00:00 2018" >, 'comments': ''}

您可以看到参与日期显示的是TYPE,而不是值。我需要明确转换吗?怎么样?

我将要作为字​​典插入到数据库中的值传递:

{'company_name': '', 'company_address': '', 'company_city': '', 'company_zip': '', 'company_country': '', 'company_phone1': '', 'company_phone2': '', 'company_description': '', 'contact_person': '', 'cp_email': '', 'cp_phone1': '', 'cp_phone2': '', 'is_training_provider': False, 'engagement_date': <wx.DateTime: "Sat Apr 28 00:00:00 2018">, 'comments': ''}

...使用此功能:

def insertRecord(targetObject, dict_values):
    print("dict values received {}".format(dict_values))
    session = connectToDatabase()
    session.execute(targetObject.__table__.insert(), dict_values)
    session.commit()
    session.close()

感谢任何指导!

1 个答案:

答案 0 :(得分:0)

在此发布我的解决方案,为了任何人的利益:

似乎缺少从wx.DateTime到Python datetime的转换。

我使用Mike的函数provided here进行转换,然后将数据字典发送到DB,如下所示:

    if isinstance(ctrlValue, wx.DateTime):
        ctrlValue = forms_controller.wxdate2pydate(ctrlValue)