[newbie] - python list into a sql query

MRAB python at mrabarnett.plus.com
Mon Mar 1 13:44:49 EST 2010


João wrote:
> Hi.
> 
> Can someone guide me into getting this to work? It's just really
> querying a DB of an Autodiscovery tool to have a bunch of updated dns
> files.
> (Thought I'm still building the first script steps) I was able to
> successfully query the DB against a single groupid, but am failing in
> passing a list of integers into the sql query.
> 
> I'm failing miserably in,
> 
> sql = '''SELECT ip, host, dns FROM zabbix_tst_db.hosts WHERE hostid IN
> (
> select hostid from hosts_groups WHERE groupid IN (' +
> ','.join(map(str, %s)) + ')''' % grp_range
> 
> 
> with
>     _mysql_exceptions.ProgrammingError: (1064, "You have an error in
> your SQL syntax; check the manual that corresponds to your MySQL
> server version for the right syntax to use near '' at line 1")
> 
[snip]
The triple-quoted string starts at:

     '''SELECT ...

and ends at:

     ...)''' % grp_range

which means that the part where you do the .join is _inside_ the string.



More information about the Python-list mailing list