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?

sj95126
  • 6,520
  • 2
  • 15
  • 34
  • 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... – Ture Pålsson Aug 15 '22 at 05:03
  • i prefer check this `https://docs.sqlalchemy.org/en/14/core/tutorial.html#executing` – Ashkan Goleh Pour Aug 15 '22 at 05: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) – JL Peyret Aug 15 '22 at 06:04
  • @TurePålsson I'm trying to convert my strings to a tuple because its needed data format for cursor.execute – Mohsen Saeidzadeh Aug 15 '22 at 08:11

2 Answers2

1
query = """select * from customers WHERE username=%s and password=%s"""
tuple1 = ("mini", 9000)
cursor.execute(query, tuple1)
mini5183
  • 48
  • 5
  • Try doing it in a cleaner way step by step, try it, if it doesn't print them and see. It will work :) – mini5183 Aug 15 '22 at 04:31
0

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

Sahil Kandroo
  • 129
  • 1
  • 3
  • 15