Google App Engine Datastore insert,select operation
Here in this post I am going to show a google app engine program which does insert operation and delete operation on google app engine datastore.
The program code is as follows:
Explanation:
The first line of the code is a simple import statement. The second,third and fourth line are for the class definition of Person which extends the db.Model class. And also we can easily understand name is of type String and age is of type Integer which are clear from db.StringProperty() and db.IntegerProperty.
The next line of code creates an instance of the class Person passing some parameters: key_name, name and age. 'key_name' is an identifier for the entity. Once the we have an instance of the class Person it can be inserted into the google app engine datastore usint the put method. The consequent two line create another instance of the class and insert into the datastore.
Now to retrieve the entities from the datastore we use the class name and all() method as show in line number 11 i.e. per=Person.all(). This line retrieves all the entities of Person type and assign it to the variable per. Finally we can loop through the variable per to get the data.
The program code is as follows:
from google.appengine.ext import db
class Person(db.Model):
name=db.StringProperty()
age=db.IntegerProperty()
person1=Person(key_name='1',name='Amit Sana',age=24)
person1.put()
person2=Person(key_name='2',name='Samantha Sharma',age=28)
person2.put()
per=Person.all()
for p in per:
print p.name
print p.age
Explanation:
The first line of the code is a simple import statement. The second,third and fourth line are for the class definition of Person which extends the db.Model class. And also we can easily understand name is of type String and age is of type Integer which are clear from db.StringProperty() and db.IntegerProperty.
The next line of code creates an instance of the class Person passing some parameters: key_name, name and age. 'key_name' is an identifier for the entity. Once the we have an instance of the class Person it can be inserted into the google app engine datastore usint the put method. The consequent two line create another instance of the class and insert into the datastore.
Now to retrieve the entities from the datastore we use the class name and all() method as show in line number 11 i.e. per=Person.all(). This line retrieves all the entities of Person type and assign it to the variable per. Finally we can loop through the variable per to get the data.
Comments
Post a Comment