I'm trying to build a python function that, given a table name and the primary key of the table, it returns true if an entry with the same primary key already exists, otherwise it returns false
Here is my attempt:
def already_esists(tablename, pm_key) :
myDB = mysql.connector.connect(
host = "localhost",
user = "user",
passwd = "password",
database = "myDatabase"
)
myCursor = myDB.cursor()
# PM_KEY is the primary key attribute of every table for which I need to use this function
query = "SELECT PM_KEY FROM %s WHERE PM_KEY = %s;"
values = [tablename, pm_key]
myCursor.execute(query, values)
ID_already_exists = myCursor.fetchall()
if ID_already_exists :
logging.warning(primary_key + " Entry already exists")
return True
else :
return False
However, once called, 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 ''table_name_example' WHERE PM_KEY = 'GELheLpr'' at line 1
GELheLpr
is for example a primary key of an entry of table_name_example
.