On Sep 24, 2010, at 7:10 PM, Jason Baker wrote:

> I'm taking data that's been encoded into schemaless JSON and inserting
> it into a table.  For simplicity's sake, let's say I have a table like
> this:
> 
> CREATE TABLE some_table(
>    id INT PRIMARY KEY,
>    insert_date DATETIME
>    );
> 
> When I get the input data, it would look something like this:
> 
> {id: 1,
> insert_date: "2008 10 29"}
> 
> The code to insert this would look something like this (obviously
> simplified - data is the above JSON structure encoded into a python
> dictionary):
> 
> metadata.reflect()
> table = metadata.tables[table_name]
> stmt = table.insert(data)
> session.execute(stmt)
> 
> In this case, insert_date would seem to be going into the database
> like this:  "0000-00-00 00:00:00".  Is there a way I can tell the
> compiler how to compile this?  Ideally, I'd like to be able to do
> something like this at the start of my program:
> 
> DATETIME.format_str = "yyyy mm dd"
> 
> ...or better yet, have a way to use python-dateutil[1] to parse the
> datetime string.  Any advice?

Most DBAPIs accept Python datetime objects directly, basically other than 
SQLite.  SQLAlchemy doesn't do any rendering or parsing of any kind for these.  
For SQLite, it still accepts only Python datetime objects, it just renders them 
into a string and then parses back to DateTime on the result side. 

If you have a stringified date format that you'd like to pass to SQL statements 
as values, such that the string is parsed into a datetime then passed to the 
DBAPI, just use TypeDecorator around DateTime: 
http://www.sqlalchemy.org/docs/core/types.html?highlight=typedecorator#sqlalchemy.types.TypeDecorator


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to