Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python检查MySQLdb中是否已存在电子邮件_Python_Mysql_Mysql Python - Fatal编程技术网

使用python检查MySQLdb中是否已存在电子邮件

使用python检查MySQLdb中是否已存在电子邮件,python,mysql,mysql-python,Python,Mysql,Mysql Python,我的目标是检查mysql数据库中是否已经存在电子邮件。我将信息从webform(字符串)拉入变量。我目前拥有以下代码: import MySQLdb db = MySQLdb.connect("localhost","username","password","databasename") cursor = db.cursor() if cursor.execute("select count(*) from registrants where email = " + "'"emailvar"

我的目标是检查mysql数据库中是否已经存在电子邮件。我将信息从webform(字符串)拉入变量。我目前拥有以下代码:

import MySQLdb

db = MySQLdb.connect("localhost","username","password","databasename")
cursor = db.cursor()
if cursor.execute("select count(*) from registrants where email = " + "'"emailvar"'") == 0:
    print "it doesn't exist"
当我试图访问该页面时,我得到一个内部服务器错误。我将错误缩小到“'”emailvar“'”,代码运行良好,但电子邮件中有“@”和“.”,这会导致SQL语法错误。我试图使用“'””打开和关闭括号来转义它们,但它不起作用并使网页崩溃。

您想要:

query = 'select count(*) from registrants where email=%s'
cursor.execute(query, emailvar)
if next(cursor, None) is None:
    # whatever

但是,我会查看应该工作的
.fetchone()
,以及SQL中存在的
(它应该有一个唯一的约束并让DB计算出来),然后返回一个布尔式的空结果。

我得到错误:builtin next=,cursor=,builtin None=None:cursor对象不是迭代器args=(‘游标对象不是迭代器’,)message='Cursor object不是迭代器'@MichaelRocca oh-我忘了它是那样工作的-试着在光标上取回
fetchone
object@JonClements:我更正了代码。我想你可能也想用一些关于准备好的语句来补充答案。@KeithGaughan谢谢-不过,请随意提供你自己的答案回答-我把这件事往后推,这样事情就没问题了-无意冒犯(但你的编辑完全改变了我的初衷)没问题。我真的不想自己回答这个问题,因为除了整个获取结果的事情,你的回答很好。