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

问题描述 投票:2回答:1

我正在尝试检查我的MYSQL表中是否存在电子邮件。我能够成功连接到数据库并在表上执行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, [email protected])

#on using quotes around string
#mycursor.execute(sqlquery, '[email protected]')
#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, [email protected])
                                  ^
SyntaxError: invalid syntax

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

python mysql mysql-python
1个回答
0
投票

您的字符串需要soubleqoutoes才能被识别为一次搅动,甚至需要使用一个元组甚至提供一个参数

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, ("[email protected]",))

我个人更喜欢import mysql.connector

它较新,并且产生的问题更少。

© www.soinside.com 2019 - 2024. All rights reserved.