I am writing a module, in which the email string is validated before it gets inserted into the db. When i try to enter invalid email string it prints else block with wrong email message but when i enter the correct email string it doesnt do anything. Here is the code:
#!/usr/bin/python
import MySQLdb
import re
# Open database connection
db = MySQLdb.connect("localhost","root","root","acl" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
def addUser(email,password):
try:
if validateEmail(email):
sql = "INSERT INTO acl_users(email, password) VALUES ('%s', '%s')" % (email, password)
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
else:
print "wrong email"
except Exception as inst:
# Rollback in case there is any error
db.rollback()
print inst
def validateEmail(email):
if len(email) > 7:
if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) != None:
return True
else:
return False
else:
return False
Any suggestions please?
edit-1
got the answer guys! after creating instance of the exception in except block i got to know that import re was missing. that solved the problem.
in validateEmail you use the module re, but you did not import it.