0Dz 0Dz - 1 month ago 4
MySQL Question

Inserting Python Variable in SQL satement

I've been trying to make the following code work but have not found a way. Im new to both MySQL and Python and connecting Python with MySQL is something i recently learned for my college project.

import mysql.connector

password = input("Enter Password Here")
print(password)

conn2 = mysql.connector.connect(host="localhost", user="root", password="root", database="trial")
c2 = conn2.cursor()
c2.execute("select lastname from table1 where lastname= '?' ", (password))
row1 = c2.fetchone()

print(row1)

if(c2==password):
print("Welcome User")
else:
print("Invalid")


My connection with the database is working just fine and the SQL statements are also executing as expected. The only problem is that when inserting the
password
variable. The
print(row1)
code outputs
None
in the Console. Please Help

Answer

Try this:

c2.execute("""select lastname from table1 where lastname= %s""", (password,))
Comments