检查 MysqlDB 中是否已存在电子邮件 Python

Check if an email already exists in MysqlDB Python

我正在尝试检查我的 MYSQL table 中是否存在电子邮件。我能够成功连接到数据库并在 table 上执行 SQL 查询。但是在检查电子邮件时,它会在 '@' 上给出 SyntaxError 作为无效语法。我试过使用转义字符('\')但它仍然给我同样的错误。

import MySQLdb

# Connect
mydb = MySQLdb.connect(host="localhost",
                    port=3000,
                     user="root",
                     passwd="xxxxxx",
                     db="xxxxxx")

sqlquery = " select * from <tablename> where email = %s"

mycursor.execute(sqlquery, abc@def.com)

#on using quotes around string
#mycursor.execute(sqlquery, 'abc@def.com')
#TypeError: not all arguments converted during string formatting

#row_count = mycursor.rowcount
#print(row_count)

输出

(kp_env) storm@storm:/mnt/d$ python abc.py
  File "abc.py", line 40
    mycursor.execute(sqlquery, abc@def.com)
                                  ^
SyntaxError: invalid syntax

此外,如果有更好的方法来检查电子邮件是否存在于 table 中,请告诉我。

您的字符串需要 doubleqoutoes 才能被识别为一个字符串,并且您需要使用一个元组甚至 fpor 一个参数

import MySQLdb

# Connect
mydb = MySQLdb.connect(host="localhost",
                    port=3000,
                     user="root",
                     passwd="xxxxxx",
                     db="xxxxxx")

sqlquery = "select * from <tablename> where email = %s"

mycursor.execute(sqlquery, ("abc@def.com",))

个人比较喜欢import mysql.connector

它更新,问题更少。