Reputation: 174
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.
Upvotes: 0
Views: 64
Reputation: 5396
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')
Upvotes: 1