gpt4 book ai didi

python - 在cursor.execute()中的sql查询中传递多个参数

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:46 25 4
gpt4 key购买 nike

我试图将多个参数传递给 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

关于python - 在cursor.execute()中的sql查询中传递多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com