如何在 python 中包含多个 cursor.execute
How to include multiple cursor.execute in python
就像我必须从 sql table 中获取来源、ID 和目的地,但它们存在于两个不同的 table 中,所以我尝试使用
dom_id=[1,10,19,31,37,42,48,57,63,64,65]
for i in range(len(dom_id)):
cursor.execute("SELECT xval, yval from table1 WHERE DOMID=?",dom_id[i])
source=""
for row in cursor.fetchall():
source=float(row[0]),float(row[1])
source=','.join(map(str, source))
cursor.execute("select t1.id as pid,pxval,pyval from table2")
ID=[]
destination=""
for row_d in cursor.fetchall():
ID.append(row_d[0])
destination = float(row_d[1]),float(row_d[2])
destination = ','.join(map(str, destination))
现在我正在使用 for-loop 来提取源代码和其他 for-loop 来提取 ID
和destination
我试过
cursor.execute(("SELECT xval, yval from table1 WHERE DOMID=?",dom_id[i]);("select t1.id as pid,pxval,pyval from table2 t1 left join (select * from table3 where clubid=?) t2 on t1.id=t2.pid where t1.cityid in (SELECT cityid FROM table1 WHERE domid = ? group by cityid) and t1.pxval>0 and t2.distance is null order by pid",(dom_id[i],dom_id[i])))
但是报错
SyntaxError: invalid syntax
有什么方法可以将两个 cursor.execute
组合在一个 for 循环中。
有什么建议吗?
我觉得可以这样做:
sqlQuery = "SELECT a.xval, a.yval,b.pid,pxval,b.pyval from table1 as a,
(select t1.id as pid,pxval,pyval from <tbl1> t1
left join ( select * from <tbl2> where clubid="+clubid+")
t2 on t1.id=t2.projectid where t1.cityid in ( SELECT cityid
FROM <tbl3> WHERE cbdid ="+cbdid+" group by cityid)
and t1.pxval>0 and t2.distance is null order by projectid)
as b WHERE a.DOMID="+dom_id[i]+"
cursor.execute(sqlQuery)
这将提供来自 table 的所需数据。
注意:如果两个 table 的行数不同,那么 tell 将是 NULL 值,您可以使用代码中的某些条件来检查它。
如果你想在一次执行中添加这个,试试这个:
cursor.execute("SELECT a.xval, a.yval,b.pid,b.pxval,b.pyval from table1 as a, (select t1.id as pid,pxval,pyval from table2 t1 left join (select * from table3 where clubid=?) t2 on t1.id=t2.pid where t1.cityid in (SELECT cityid FROM table1 WHERE domid = ? group by cityid) and t1.pxval>0 and t2.distance is null order by pid) as b WHERE a.DOMID=?",dom_id[i],dom_id[i],dom_id[i])
希望这对您有所帮助。
就像我必须从 sql table 中获取来源、ID 和目的地,但它们存在于两个不同的 table 中,所以我尝试使用
dom_id=[1,10,19,31,37,42,48,57,63,64,65]
for i in range(len(dom_id)):
cursor.execute("SELECT xval, yval from table1 WHERE DOMID=?",dom_id[i])
source=""
for row in cursor.fetchall():
source=float(row[0]),float(row[1])
source=','.join(map(str, source))
cursor.execute("select t1.id as pid,pxval,pyval from table2")
ID=[]
destination=""
for row_d in cursor.fetchall():
ID.append(row_d[0])
destination = float(row_d[1]),float(row_d[2])
destination = ','.join(map(str, destination))
现在我正在使用 for-loop 来提取源代码和其他 for-loop 来提取 ID
和destination
我试过
cursor.execute(("SELECT xval, yval from table1 WHERE DOMID=?",dom_id[i]);("select t1.id as pid,pxval,pyval from table2 t1 left join (select * from table3 where clubid=?) t2 on t1.id=t2.pid where t1.cityid in (SELECT cityid FROM table1 WHERE domid = ? group by cityid) and t1.pxval>0 and t2.distance is null order by pid",(dom_id[i],dom_id[i])))
但是报错
SyntaxError: invalid syntax
有什么方法可以将两个 cursor.execute
组合在一个 for 循环中。
有什么建议吗?
我觉得可以这样做:
sqlQuery = "SELECT a.xval, a.yval,b.pid,pxval,b.pyval from table1 as a,
(select t1.id as pid,pxval,pyval from <tbl1> t1
left join ( select * from <tbl2> where clubid="+clubid+")
t2 on t1.id=t2.projectid where t1.cityid in ( SELECT cityid
FROM <tbl3> WHERE cbdid ="+cbdid+" group by cityid)
and t1.pxval>0 and t2.distance is null order by projectid)
as b WHERE a.DOMID="+dom_id[i]+"
cursor.execute(sqlQuery)
这将提供来自 table 的所需数据。
注意:如果两个 table 的行数不同,那么 tell 将是 NULL 值,您可以使用代码中的某些条件来检查它。
如果你想在一次执行中添加这个,试试这个:
cursor.execute("SELECT a.xval, a.yval,b.pid,b.pxval,b.pyval from table1 as a, (select t1.id as pid,pxval,pyval from table2 t1 left join (select * from table3 where clubid=?) t2 on t1.id=t2.pid where t1.cityid in (SELECT cityid FROM table1 WHERE domid = ? group by cityid) and t1.pxval>0 and t2.distance is null order by pid) as b WHERE a.DOMID=?",dom_id[i],dom_id[i],dom_id[i])
希望这对您有所帮助。