简体   繁体   English

web.py数据库选择访问权限

[英]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) ,看看为你得到的对象定义了哪些成员变量和函数。

  • If you see something like __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)将结果转换为列表,并且通常在循环中迭代对象。
  • If you see something like __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将是一个存储liststoragedict相同,但您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM