0

I created a function called "display_table" to display any table , given its name. When I call this function to display a table called 'members', I get the following error.

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in >your SQL syntax; check the manual that corresponds to your MySQL server >version for the right syntax to use near ''members'' at line

def display_table(table_name):
    sql = " select * from %s "
    val = (table_name,)
    mycursor.execute(sql,val)
    myresult = mycursor.fetchall()
    for row in myresult:
        print(row)


display_table('members')

I can't understand why this error shows up. Please help.

1 Answer 1

1

You cannot pass a table name as a parameter to your prepared statement. Parameters are only allowed for column data. You should change your code:

def display_table(table_name):
    sql = " select * from %s " % table_name
    mycursor.execute(sql)
    myresult = mycursor.fetchall()
    for row in myresult:
        print(row)


display_table('members')
Sign up to request clarification or add additional context in comments.

Comments

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.