简体   繁体   English

Python with sqlalchemy db retrival

[英]Python with sqlalchemy db retrival

I am quite new to python and to sqlalchemy.I have written the following netowrk program. 我是python和sqlalchemy的新手。我编写了以下netowrk程序。

class SourcetoPort(Base):
    """"""
    __tablename__ = 'source_to_port'
    id = Column(Integer, primary_key=True)
    port_no        = Column(Integer)
    src_address    = Column(String)

    #----------------------------------------------------------------------
    def __init__(self, src_address,port_no):
        """"""
        self.src_address = src_address
        self.port_no     = port_no
#
if session.query(SourcetoPort).filter_by(src_address='packet.dst').all():

In the above code the line 在上面的代码行

 if session.query(SourcetoPort).filter_by(src_address='packet.dst').all(): 

does not work as I expect it.What I expect is that it should retrive the entry from sqlalchemy database and if it succeeds (as the output is not NONE) it should execute the following code. 没有按照我的预期工作。我期望它应该从sqlalchemy数据库中检索条目,如果成功(因为输出不是NONE),它应该执行以下代码。

When I try to print that line using print 当我尝试使用print打印该行时

session query  []

The output that I get is 我得到的输出是

 session query [] 

Am I doing something wrong.It would be great if someone could point this out. 我做错了什么。如果有人能指出这一点,那就太好了。

If I change the above line based on the suggestion as 如果我根据建议更改上述行

creating a new db entry
RECIEVED FROM PORT  2 SOURCE  96:74:ba:a9:92:b9
creating a new db entry

ERROR:core:Exception while handling Connection!PacketIn...
Traceback (most recent call last):
  File "ws_thesis/pox/pox/lib/revent/revent.py", line 234, in raiseEventNoErrors
    return self.raiseEvent(event, *args, **kw)
  File "ws_thesis/pox/pox/lib/revent/revent.py", line 281, in raiseEvent
    rv = event._invoke(handler, *args, **kw)
  File "ws_thesis/pox/pox/lib/revent/revent.py", line 159, in _invoke
    return handler(self, *args, **kw)
  File "ws_thesis/pox/tutorial.py", line 137, in _handle_PacketIn
    self.act_like_switch(packet, packet_in)
  File "ws_thesis/pox/tutorial.py", line 101, in act_like_switch
    print session.query(SourcetoPort).filter_by(src_address=packet.dst).count()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2400, in count
    return self.from_self(col).scalar()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2045, in scalar
    ret = self.one()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2014, in one
    ret = list(self)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2057, in __iter__
    return self._execute_and_instances(context)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2072, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1405, in execute

I get the following error: 我收到以下错误:

 creating a new db entry RECIEVED FROM PORT 2 SOURCE 96:74:ba:a9:92:b9 creating a new db entry ERROR:core:Exception while handling Connection!PacketIn... Traceback (most recent call last): File "ws_thesis/pox/pox/lib/revent/revent.py", line 234, in raiseEventNoErrors return self.raiseEvent(event, *args, **kw) File "ws_thesis/pox/pox/lib/revent/revent.py", line 281, in raiseEvent rv = event._invoke(handler, *args, **kw) File "ws_thesis/pox/pox/lib/revent/revent.py", line 159, in _invoke return handler(self, *args, **kw) File "ws_thesis/pox/tutorial.py", line 137, in _handle_PacketIn self.act_like_switch(packet, packet_in) File "ws_thesis/pox/tutorial.py", line 101, in act_like_switch print session.query(SourcetoPort).filter_by(src_address=packet.dst).count() File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2400, in count return self.from_self(col).scalar() File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2045, in scalar ret = self.one() File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2014, in one ret = list(self) File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2057, in __iter__ return self._execute_and_instances(context) File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2072, in _execute_and_instances result = conn.execute(querycontext.statement, self._params) File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1405, in execute 

If you want to filter items by the contents of a variable , you need to use that variable directly: 如果要按变量内容过滤项目,则需要直接使用该变量:

session.query(SourcetoPort).filter_by(src_address=packet.dst)

You were instead trying to filter by the string 'packet.dst' . 您试图通过字符串'packet.dst'进行过滤。

You made the same mistake when creating the SourcetoPort entry; 您在创建 SourcetoPort条目时犯了同样的错误; you probably wanted to store result of the packet.src expression, not the string 'packet.src' : 你可能想存储packet.src表达式的结果,而不是字符串'packet.src'

SourcetoPort(src_address=packet.src, port_no=packet_in.in_port)

Note that calling .all() retrieves all matching entries from the database, but you are only using it in a if test. 请注意,调用.all()从数据库中检索所有匹配的条目,但您只在if测试中使用它。 It would be more efficient (potentially much more so) to use .count() instead of .all() to let the database tell us how many items match: 这将是更有效的(可能更加如此)使用.count()代替.all()让数据库告诉我们很多项目如何匹配:

if session.query(SourcetoPort).filter_by(src_address=packet.dst).count():

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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