在我的MySQL数据库中,我有10个表。根据用户输入,我想从表之一中选择行。用户输入是图像。我正在获取图像具有的像素列和行数,并将其存储在变量中。

rows,cols,channels = img.shape
w = int(cols)
h = int(rows)
l = w-10
m = w+10
p = h-10
q = h+10
area = w*h


mysql表是根据图像的区域创建的,因此,根据该区域,我想从特定表中选择值。

if (area < 110980):
        sql = "select * from adf1 where WIDTH between %s and %s and HEIGHT between %s and %s;",(l,m,p,q)
    elif (area < 182520):
        sql = "select * from adf2 where WIDTH between %s and %s and HEIGHT between %s and %s;",(l,m,p,q)
else:
        sql = "select * from adf10 where WIDTH between %s and %s and HEIGHT between %s and %s;",(l,m,p,q)
cur = mysql.connect().cursor()
cur.execute(sql)


我收到以下错误:

TypeError: cannot concatenate 'str' and 'tuple' objects


但是,当我在cur.execute中传递单个查询时,便可以检索结果。

cur.execute("select * from adf10 where WIDTH between %s and %s and HEIGHT between %s and %s;",(l,m,p,q))

最佳答案

当前变量sql是元组,因此如果要将其作为参数传递给execute,则应使用*

cur.execute(*sql)


您可以在文档unpacking-argument-lists中阅读更多内容

关于python - Python为MySQL创建动态选择语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51925097/

10-14 13:07
查看更多