Converting from local -> UTC

Gabriel Genellina gagsl-py2 at
Fri Jul 18 01:26:11 CEST 2008

En Wed, 16 Jul 2008 15:00:50 -0300, Keith Hughitt  
<keith.hughitt at> escribi�:

> Thanks Gabriel!
> That helps clear things up for me. The above method works very well. I
> only have one remaining question:
> How can I pass a datetime object to MySQL?'
> So far, what I've been doing is building the query as a string, for
> example:
> query = "INSERT INTO image VALUES(%d, %d, %s, '%s')" % (id, meas,
> date, 'jpg')
> cursor.execute(query)

That's not a good idea, in general (among other problems: what if any text  
contains a quote? ever heard of "sql injection"?). Use this form instead:

query = "INSERT INTO image VALUES(%s, %s, %s, %s)"
cursor.execute(query, (id, meas, date, 'jpg'))

Note that I used %s everywhere (it's just a placeholder, not a format) and  
the execute method receives two arguments, the second being a tuple  
containing the desired values.

Gabriel Genellina

More information about the Python-list mailing list