2

I have used this code to use two variables in a single SQL code in Python :

cursor.execute("select * from customers WHERE username=%s and password=%s", (a, b))

but I've got this error :

MySQLInterfaceError: Python type tuple cannot be converted

though I've converted my strings into a tuple like this:

a = tuple(map(str, emaile.split(",")))
b = tuple(map(str, passe.split(",")))

how can I use these two variables in my cursor.execute code?

4
  • 1
    Could you describe in a bit more detail what you are trying to do? From the split() stuff, it looks like you are dealing with comma-separated lists of addresses and passwords, but I see nothing dealing with lists in the SQL... Commented Aug 15, 2022 at 5:03
  • i prefer check this https://docs.sqlalchemy.org/en/14/core/tutorial.html#executing Commented Aug 15, 2022 at 5:05
  • Your problem is not the 2 variables, it is that the variables are themselves tuples. Also, the notion of using passwords to query a db is… disturbing security-wise and probably pointless sql-wise if customer is keyed by username. You probably need to use a loop. or username in (…) which can be tricky with binds. And doesnt work with another list for another var (that password) Commented Aug 15, 2022 at 6:04
  • @TurePålsson I'm trying to convert my strings to a tuple because its needed data format for cursor.execute Commented Aug 15, 2022 at 8:11

2 Answers 2

1
query = """select * from customers WHERE username=%s and password=%s"""
tuple1 = ("mini", 9000)
cursor.execute(query, tuple1)
Sign up to request clarification or add additional context in comments.

1 Comment

Try doing it in a cleaner way step by step, try it, if it doesn't print them and see. It will work :)
0

cursor.execute("INSERT INTO table VALUES (%s, %s, %s)", (var1, var2, var3))

2 Comments

Sahil as you see my variables are not in series i need to have an "And" between them so how should i mention them after ","

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.