问题
I have a table that I am trying to insert many using a dict object.
engine = create_engine('sqlite:///:memory:', echo=True)
metadata = MetaData()
hockey= Table('hockey', metadata,
Column('team', String(16), primary_key=True),
Column('jersey_colour', String(16)),
Column('stadium', String(32)),
Column('goals', Integer),
Column('date', Date, primary_key=True),
Column('assists', Integer))
>>>data[0]
{'jersey_colour': u'Blue',
'team': u'Toronto Maple Leafs',
'stadium': u'Air Canada Center',
'date': '2013-03-25',
'assists': 301,
'goals': 151}
>>>data[1]
{'jersey_colour': u'Red',
'team': u'Calgary Flames',
'stadium': u'PenWest',
'date': '2013-03-25',
'assists': 254,
'goals': 147}
conn = engine.connect()
conn.execute(ins, data)
StatementError: SQLite Date type only accepts Python date objects as input. (original cause: TypeError: SQLite Date type only accepts Python date objects as input.) 'INSERT INTO "hockey" (team, jersey_colour, stadium, goals, date, assists) VALUES (?, ?, ?, ?, ?, ?)' [{'jersey_colour': 'Blue', 'team': 'Toronto Maple Leafs', 'stadium': u'Air Canada Center', 'date': '2013-03-25', 'assists': 301, 'goals': 151}
First question - does the order by which the parameters are fed to '?' matter. As you can see, the fields appear as follows:
team jersey_colour stadium goals date assistsHOWEVER, the dictionary order is arbitrary:
jersey_colour team stadium date assists goalsIs this a problem for sqlite or mysql? In theory the key is provided so it should be able to figure where to store the data but I'm not sure. For production I will be using a mysql database but for testing I am using the inmemory sqlite db.
Second question, the error:
StatementError: SQLite Date type only accepts Python date objects as inputI found the following (https://groups.google.com/forum/?fromgroups=#!topic/sqlalchemy/EeZbkePPb38), which says:
SQLite doesn't have a DATE type specifically. SQLAlchemy's Date() type expects Python datetime, i.e. "import datetime; date = datetime.date(year, month day)".
The value that is paired with the data key is created by the Python datetime.date command as per what the commentator said above.
>>>today_date = datetime.date.today()
So I'm not sure why the error is occurring. I appreciate your help on both questions #1 and #2. Thank you.
回答1:
SQLAlchemy takes care of the bind parameters and ordering. There is no need to worry about that.
You specified in your SQLAlchemy ORM metadata definition that you are handling a date column. SQLAlchemy then handles the translation to the correct form for the backend database. The error comes from SQLAlchemy, not SQLLite; SA translates date objects for you.
This goes both ways; even when using SQLLite, you can put date objects in, and when you query, you get date objects out again.
The reply to the post you link to talks about the same things there; use a String() column if you wanted to store dates as strings, use datetime.date() or datetime.datetime() instances instead if you declare the column to be of type Date().
Your date key is a string; use a datetime.date() object instead to avoid the error:
{'jersey_colour': u'Blue',
'team': u'Toronto Maple Leafs',
'stadium': u'Air Canada Center',
'date': datetime.date(2013, 3, 25),
'assists': 301,
'goals': 151}
来源:https://stackoverflow.com/questions/15644808/insertmany-into-the-in-memory-sqllite-db-sqlite-date-type-only-accepts-python