views:

87

answers:

1

I want to check if an email is in my database in Appengine, and if not: then enter it into the datastore.

I am new to python. Why is this simple code not working? (Also If there is a better way/more efficient way to write this, please tell me)

(I get the error: BadArgumentError: Unused positional arguments [1])

class EmailAdd(webapp.RequestHandler):
def get(self):
    query = db.GqlQuery("SELECT * FROM EmailDatabase WHERE emailaddress=':1'", self.request.get('emailaddress'))
    result = query.get()
    if result is None:
        newemail = EmailDatabase()
        newemail.emailaddress = self.request.get('emailaddress')
        newemail.put()

And for reference, this is my db class:

class EmailDatabase(db.Model):
     emailaddress = db.StringProperty()
     date = db.DateTimeProperty(auto_now_add=True)
+3  A: 

You don't need to use quotes when binding a parameter to the query:

query = db.GqlQuery("SELECT * FROM EmailDatabase WHERE emailaddress = :1", self.request.get('emailaddress'))

Otherwise it will read it as a string and actually only return objects that have :1 as their emailaddress value.

Also, make sure you validate the user input (self.request.get('emailaddress')) before inserting it into the query.

tomlog
thank you, tomlog. thank you. you are an expert python/gae programmer...
chris