在 cursor.execute() 中的 sql 查询中传递多个参数
Passing multiple parameters in sql query in cursor.execute()
我正在尝试将多个参数传递给 sql 查询,但它让我出错,
这里是查询
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]))
它给我错误
ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 't2'. (102) (SQLExecDirectW)")
但是我查了查不出问题所在,
对此的任何建议都会有所帮助。
谢谢
如错误消息所示,您的 SQL 命令文本格式错误。为清楚起见重新格式化,您的查询是
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)
请注意,<tbl2>
子查询没有右括号。您的查询可能应该是
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
我正在尝试将多个参数传递给 sql 查询,但它让我出错,
这里是查询
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]))
它给我错误
ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 't2'. (102) (SQLExecDirectW)")
但是我查了查不出问题所在,
对此的任何建议都会有所帮助。
谢谢
如错误消息所示,您的 SQL 命令文本格式错误。为清楚起见重新格式化,您的查询是
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)
请注意,<tbl2>
子查询没有右括号。您的查询可能应该是
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