终于解决了Python多个字段多个参数的查询
def selectby(self,sql,*args):
self.cursor = self.db.cursor()
try:
ute(sql,args)
tt=ute(sql,args)
data = self.cursor.fetchall()
return data
except:
llback()
return False
finally:
self.cursor.close()
self.db.close()
def select_info3():
db = MysqldbHelper()
global case
case = case + 1
try:
modelid = ("modelid")
sceneid = ("sceneid")
#id=[1,2]
sql_select = 'select * from test.info where modelid in %s and sceneid in %s'
#sql_select ='select * from test.info where modelid in ({}) and sceneid in ({})'.format(",".join(['%s' for i in modelid]),",".join(['%s' for j in sceneid])) tt=sql_select
print(tt)
data=[modelid,sceneid]
fc = db.selectby(sql_select,modelid,sceneid)
#data1 = transinfo(fc)
txturl=[]
txtpara=[]
for i in range(len(fc)):
txturl.append(fc[i][4])
txtpara.append(fc[i][6])
db = MysqldbHelper()
sql_insert='insert sultinfo(caseinfo,caseid) value(%s,%s)'
insertdata=[case,fc[i][0]]
db.insert(sql_insert,insertdata)
data={'data':fc,"code":200}
return data
except:
data={"code":500}
sql自学难吗return json.dumps(data, ensure_ascii=False, indent=4)
sql_select ='select * from test.info where modelid in ({}) and sceneid in ({})'.format(",".join(['%s' for i in modelid]),",".join(['%s' for j in sceneid]))
原本以为需要⽤上述sql语句拼接,结果发现拼接太难,还是⽤简单的⽅法来吧,即:
modelid = ("modelid")
sceneid = ("sceneid")
sql_select = 'select * from test.info where modelid in %s and sceneid in %s'
fc = db.selectby(sql_select,modelid,sceneid)
拼接传参测试:
def test():
values = [43, 44]
values2 = [4]
sql_select = 'select id from test.info where modelid in ({}) and sceneid in ({})'.format( ",".join(['%s' for i in values]), ",".join(['%s' for i in values2]))
tt = sql_select
print(tt)
def test1():
values = [1, 2]
sql = "select * from asset where domain in ({})".format(",".join(['%s' for i in values])) print(sql)
print(values)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论