[英]web.py database select access
I have been messing around with web.py lately and wanted to grab some stuff from a db and its returning me a "Storage" object. 我最近一直在搞乱web.py,想从数据库中获取一些东西,并返回给我一个“存储”对象。 an the code i am using to call my info is: 我用来调用我的信息的代码是:
db = web.database(dbn='sqlite', db='sqlfile.sqlite')
sely = db.select('carp', order="id ASC")
when sely runs it drops me out text like so: 当它运行时它会让我掉出文字,就像这样:
<Storage {'lvl': 0, 'symbol': u'formb', 'logged': u'false', 'id': 1, 'display': u'Classic'}>
when you print out sely the storage line comes out. 当你打印出sely时,存储线就会出来。 how can i get the dictionary out of this object? 我如何从这个对象中获取字典?
A general Python trick for dealing with unknown APIs is to use the dir
builtin . 处理未知API的一般Python技巧是使用dir
内置 。 Try dir(sely)
in the interpreter to see what member variables and functions are defined for the object you get. 在解释器中尝试dir(sely)
,看看为你得到的对象定义了哪些成员变量和函数。
__iter__
, you can call list(sely)
to convert the results to a list, and generally iterate over the object in a loop. 如果你看到类似__iter__
东西,你可以调用list(sely)
将结果转换为列表,并且通常在循环中迭代对象。 __getitem__
, then you can index into the object and hope to get a value back. 如果你看到类似__getitem__
东西,那么你可以索引到对象并希望得到一个值。 As a side note, I just tried out your code and I get sely
to be a web.utils.IterBetter
instance (which returns 0 rows, instead of expected 3 in my case). 作为一个方面说明,我只是想你的代码,我得到sely
是一个web.utils.IterBetter
实例(返回0行,而不是在我的情况下,预计3)。 So I cannot really reproduce your problem (but have problems of my own, so to speak). 所以我无法真正重现你的问题(但我自己也有问题,可以这么说)。
db = web.database(dbn='sqlite', db='sqlfile.sqlite')
sely = db.select('carp', order="id ASC").list()
sely
would be a list
of storages, storage
is the same as dict
, but you can access arguments with obj.key
, instead of obj["key"]
. sely
将是一个存储list
, storage
与dict
相同,但您可以使用obj.key
而不是obj["key"]
访问参数。 You can do dict(obj)
to convert storage
into dict
. 您可以执行dict(obj)
将storage
转换为dict
。
in windows 在窗户
return list(db.select('top',what='score',where="name = $id",vars=locals())
is ok. 没关系。 you can get the score's value. 你可以得到分数的价值。
but 但
in ubuntu 在ubuntu
you shuld do it like this 你这样做吧
db.select('top',what='score',where="name = $id",vars=locals())[0]["score"]
i don't know why but it works in my computer 我不知道为什么,但它在我的电脑上工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.