【发布时间】:2021-07-28 20:42:50
【问题描述】:
我正在尝试在我的数据库管理器类中实现一个函数,该函数检查我的 MySQL 表中是否存在(通过他们的电子邮件)行(用户)。
见下面的代码:
def has_user(self, table_name : str, user_credentials: UserCredentials) -> bool:
mysql_hasuser_query = """
SELECT COUNT(1) FROM {t_name} WHERE email = {u_email}
""".format(t_name=table_name, u_email=user_credentials.email)
cursor = self.connection.cursor()
cursor.execute(mysql_hasuser_query)
if cursor.fetchone()[0]:
print("User exists in database!")
return True
我收到以下语法错误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 '@gmail.com' at line 1
但是,我在 MySQL 查询编辑器中实现了这一点,并且运行良好。我不确定我在这里做错了什么。
【问题讨论】: