Domnick
Domnick

Reputation: 519

Passing multiple parameters in sql query in cursor.execute()

I m trying to pass multiple parameters to sql query but it is throwing me with error,

here is the query

cbd_id=[1,10,19,31,37,42,48,57,63,64,65]


cursor.execute('''select t1.id as pid,pxval,pyval from <tbl1> t1 left join (select * from <tbl2> where clubid=? t2 on t1.id=t2.projectid where t1.cityid in (SELECT cityid FROM <tbl3> WHERE cbdid =? group by cityid) and t1.pxval>0 and t2.distance is null order by projectid)''',(cbd_id[i],cbd_id[i]))

And it give me error

ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 't2'. (102) (SQLExecDirectW)")

But I checked couldn't identify the problem,

Any suggestion on this will be helpful.

Thanks

Upvotes: 1

Views: 3286

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123829

As the error message suggests, your SQL command text is malformed. Reformatted for clarity, your query is

select t1.id as pid,pxval,pyval 
from 
    <tbl1> t1 
    left join 
    (
        select * 
        from <tbl2> 
        where clubid=? 
    t2 
        on t1.id=t2.projectid 
where 
    t1.cityid in (
            SELECT cityid 
            FROM <tbl3> 
            WHERE cbdid =? 
            group by cityid) 
    and 
    t1.pxval>0 
    and 
    t2.distance is null 
order by projectid)

Notice that the <tbl2> subquery does not have its closing parenthesis. Your query probably should be

select t1.id as pid,pxval,pyval 
from 
    <tbl1> t1 
    left join 
    (
        select * 
        from <tbl2> 
        where clubid=? 
    ) t2 
        on t1.id=t2.projectid 
where 
    t1.cityid in (
            SELECT cityid 
            FROM <tbl3> 
            WHERE cbdid =? 
            group by cityid) 
    and 
    t1.pxval>0 
    and 
    t2.distance is null 
order by projectid

Upvotes: 1

Related Questions