Google App Engine python,GQL,仅从数据存储中选择一列
我试图从数据存储表中仅提取一列,
我有一个 Books 模型 id、key、title、author、isbn 和price
everything = db.GqlQuery('SELECT * FROM Books') 给了我一切,但说我只想要书名
books = db.GqlQuery('SELECT title FROM Books')
我尝试过人们建议的一切,但似乎都不起作用
非常感谢任何帮助 谢谢
Im trying to pull only one column from a datastore table
I have a Books model with
id, key, title, author, isbn and price
everything = db.GqlQuery('SELECT * FROM Books') gives me everything, but say i only want the title
books = db.GqlQuery('SELECT title FROM Books')
Ive tried everything people have suggested but nothing seems to work
Any help is much appreciated
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。 GQL 不是 SQL,数据存储也不是关系数据库。实体存储为单个序列化协议缓冲区,并且不可能获取实体的一部分;整个事情需要反序列化。
You can't. GQL is not SQL, and the datastore is not a relational database. An entity is stored as a single serialized protocol buffer, and it's impossible to fetch part of an entity; the whole thing needs to be deserialized.
数据存储中的一个设计考虑因素是使用在关系数据库中不会使用的冗余。
例如,您的代码可能有一个“标题”实体,每次添加一本书时都会添加该实体,并在每次删除一本书时删除该实体。然后,您可以查询 Titles 实体以获取所有标题,而无需加载所有书籍。
您的代码必须强制执行这些规则,但是通过一些 Python 抽象,这并不困难(即将对书籍或标题的所有访问放在一个类后面,该类的方法强制执行数据中的关系)。
我强烈建议您在此处阅读有关数据存储的一些更高级的主题。
人们经常将领悟时刻称为“我如何学会停止担忧并热爱数据存储”。它们基本上是人们从正常形式的数据思维方式转变为分布式/冗余视图的时刻,您可以在数据存储中喷射数据,它会处理它。
A design consideration in the datastore is to use redundancy where you wouldn't in a relational database.
So for example, your code could have a entities for "Titles" that it adds each time a book is added, and removes each time a book is removed. Then you could query the Titles entities to get all the titles without having to load all the books.
Your code has to enforce those rules, but with a bit of python abstraction, that's not difficult (i.e. put all accesses to books or titles behind a class whose methods enforce the relationships in your data).
I highly suggest reading some of the more advanced topics on the data store here.
People often refer to moments of realization as "How I learned to stop worrying and love the data store." They are basically moments when people switch out of a Normal Form way of thinking about data, and into a distributed/redundant view where you can just spray data at the data store and it'll handle it.