Ordering of dict keys & values
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Mon Aug 3 21:18:08 EDT 2009
En Mon, 03 Aug 2009 17:47:23 -0300, Wells Oliver <wells at submute.net>
escribió:
> I understand that the keys in a dictionary are ordered not randomly but
> something practically close to it, but if I create a SQL query like so:
>
> query = 'INSERT INTO Batting (%s) VALUES(%s)' % (','.join(stats.keys()),
> ','.join(stats.values()))
>
> Can I at least rely on the value being in the same index as its
> corresponding key?
You already got the answer you wanted, but note that building a SQL
statement that way is unsafe [1]. I prefer this way:
query= 'INSERT INTO Batting (%s) VALUES(%s)' % (
','.join(stats.keys()),
','.join(['?']*len(stats)))
cursor.execute(query, stats.values())
[1] If you don't know what "SQL injection" means, see http://xkcd.com/327/
--
Gabriel Genellina
More information about the Python-list
mailing list