开发者

Does Google Webapp framework for Google App Engine maintain a link to the database?

开发者 https://www.devze.com 2022-12-14 21:36 出处:网络
1) When a script gets data from the开发者_Go百科 database using the db.Model.get_element_by_id(\"id\") method, what id does it refer to, and how can you get it from the database.

1) When a script gets data from the开发者_Go百科 database using the db.Model.get_element_by_id("id") method, what id does it refer to, and how can you get it from the database.

2) If you get a result using this method, does that result maintain a link to the database so that any changes to the result are reflected on the database? If not, how would you update an entry in the database?


As Jonathan Feinberg suggested, this is answered in the Google App Engine tutorial. The relevant piece of the tutorial is found here: http://code.google.com/appengine/docs/python/gettingstarted/usingdatastore.html

On that page, this text specifically answers your question about "how would you update an entry in the database:

Finally, greeting.put() saves our new object to the datastore. If we acquired this object from a query, put() would update the existing object. Since we created this object with the model constructor, put() adds the new object to the datastore.

The explanation for keys and such is found on this documentation page: http://code.google.com/appengine/docs/python/datastore/keysandentitygroups.html

And as a final suggestion, don't worry about things like "maintaining a connection to the database". The entire premise of the Google App Engine is that they have abstracted away things like connection management, and such, so that you can benefit from the inherent scalability of the platform. Sticking to the documentation and learning about the libraries and frameworks that are available will be the best route to success.


Every entity in the db can have either a numeric ID or a key name; the get_by_id class method can actually fetch by either, depending on whether you pass it a number (then it looks for that ID) or a string (then it looks for that key name). (You can also pass it a list which contains numbers, numbers and strings, etc).

Given an entity x, you can get a Key object k by k = x.key(), then k.id() returns the numeric ID of entity x (or None if the entity has no numeric ID), k.name() returns the name or None (you can also check if either is present by if k.has_id_or_name():, and get whichever of the two is present, or None if neither are, by k.id_or_name().

There is no "link"; the ID or key name can be thought of as the one and only "primary key" to the entity in the DB (actually it automatically gets mixed with some other bits of metadata like the app's name;-). When you put an existing entity (after getting it and changing something) that primary key lets the DB overwrite the existing entity rather than creating a new one.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号