JustAimz
JustAimz

Reputation: 1

fetching() return None even if the row exist

Code:

import mysql.connector
import tkinter
from tkinter import messagebox
#This code is to hide the main tkinter window
root = tkinter.Tk()
root.withdraw()

mydb = mysql.connector.connect(
host="myip",
user="generator",
password="pass",
database="passwords"
)

mycursor = mydb.cursor()

username = "robert"

query = "SELECT * FROM password WHERE username = '%s' AND service = '%s'"
mycursor.execute(query, username, "Netflix")

myresult = mycursor.fetchone()

print("%s\n" % myresult)

Result:

/usr/bin/env /usr/local/bin/python3 /Users/pana/.vscode/extensions/ms-python.py
thon-2022.6.2/pythonFiles/lib/python/debugpy/launcher 56641 -- /Users/pana/Desktop/Password\ Saver/reader.py
None

I don't know where is the problem. If I remove one of the SQL conditions and use fetchall() it works. (And remove one of the columns in SQL as well)

Upvotes: 0

Views: 32

Answers (1)

nacho
nacho

Reputation: 5397

That is not the way to use parametrized queries. You should send only one parameter as a tuple or list. And you don´t need the quotation marks.

query = "SELECT * FROM `password` WHERE username = %s AND service = %s"
mycursor.execute(query, (username, "Netflix"))

Upvotes: 1

Related Questions