How to enter item into Google AppEngine Datastore?
-
23-09-2019 - |
Question
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)
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow