-1

In my MySQL database I have 10 tables. Based on the user input, I want to select the rows from one of the table. The user input is an image. I am getting the number of columns and rows of pixels the image has and storing it in a variable.

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

the mysql tables are created basis the area of the image, and hence, depending on the area, I want to select the values from a specific table.

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)

I am getting the following error:

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

However, when I pass on a single query within cur.execute I am able to retrieve the results.

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

2 Answers 2

1

current your variable sql is tuple, so if you want to pass it as parameters to the execute you should use *

cur.execute(*sql)

you can read more in the doc unpacking-argument-lists

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer and the document link, this works...will accept your answer in 5 mins
glad to help you
0

this should fix your problem. You need to use % not comma in the queries inside if else.

"select * from adf2 where WIDTH between %s and %s and HEIGHT between %s and %s;"%(l,m,p,q)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.