Retrieve created_at timestamp from pgsql
Question: I'm a Python newbie. I wrote an sql query to retrieve created_at timestamp in
pgsql. When I called the method `strftime('%x')` on it, I got this error:

    
    
    AttributeError: 'long' object has no attribute 'strftime'
    

This is the query:

    
    
    SELECT created_at FROM rating WHERE user_id = 'xxxxx' ORDER BY id DESC LIMIT 2;
    

When I printed the result of the query, I merely got `[(3L,)]` which is just
one of the two created_at times expected. How do I convert this back to
python's datetime?


Answer: strftime looks like it's not callable, have you imported DateTime?

Also, when calling strftime you'll need to format it, for example
created_at.strftime('%y %B %d').

Finally, it's actually quicker to process and convert the time in SQL rather
than using strftime.

A simpler and more performant solution would be to just format in the SQL
itself:

    
    
    SELECT to_char(created_at,'YY-MM-DD HH24:MI:SS') FROM rating WHERE user_id = 'xxxxx' ORDER BY id DESC LIMIT 2;
    

