-1
import mysql.connector

mydb = mysql.connector.connect(host= 'localhost', user='root', passwd="barbie", db='data')

mycursor = mydb.cursor() #execute, fetch data act as a pointer
print("Connected to database")
query = ("select name , matric from users where username = %s")
username = 'shawn'
mycursor.execute(query, (username))
print ("Fetching single row")
record  = mycursor.fetchone
print (record)

The errors

Why do I get this kind of error? I only want to display the row.

The table from my database

0

1 Answer 1

0

The error is how you are passing the parameters to execute. execute takes an iterable (list/tuple) but you are passing a single value. The following 2 lines are equivalent

(username)
username

To make a tuple with only 1 element you need to add a comma (,)

mycursor.execute(query, (username, ))
Sign up to request clarification or add additional context in comments.

3 Comments

why do i get the output 'Connected to database Fetching single row <bound method CMySQLCursor.fetchall of <mysql.connector.cursor_cext.CMySQLCursor object at 0x0000020DEDF33630>>' ?
i want to be able to display their name and matric
You need to call fetchone - record = mycursor.fetchone()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.