【问题标题】:error in sqlalchemy after replacing count query with exists query [duplicate]用存在查询替换计数查询后sqlalchemy出错[重复]
【发布时间】:2013-05-03 23:24:37
【问题描述】:
class SourcetoPort(Base):
    """"""
    __tablename__ = 'source_to_port'
    id = Column(Integer, primary_key=True)
    port_no        = Column(Integer)
    src_address    = Column(String,index=True)

    #----------------------------------------------------------------------
    def __init__(self, src_address,port_no):
        """"""
        self.src_address = src_address
        self.port_no     = port_no


  def act_like_switch (self, packet, packet_in):
    """
    Implement switch-like behavior.
    """
    # Learn the port for the source MAC
    #print "RECIEVED FROM PORT ",packet_in.in_port , "SOURCE ",packet.src
    # create a Session
    #Session = sessionmaker(bind=engine)
    #session = Session()
    self.mac_to_port[packet.src]=packet_in.in_port
    #if self.mac_to_port.get(packet.dst)!=None:
    print "count for dst",session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count(),str(packet.dst)
    #if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():
    if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar() is not None:
           #send this packet
           print "got info from the database"
           q_res = session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).first()
           self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, packet_in.in_port)
           #create a flow modification message
           msg = of.ofp_flow_mod()
           #set the fields to match from the incoming packet
           msg.match = of.ofp_match.from_packet(packet)
           #send the rule to the switch so that it does not query the controller again.
           msg.actions.append(of.ofp_action_output(port=q_res.port_no))
           #push the rule
           self.connection.send(msg)
    else:
           #flood this packet out as we don't know about this node.
           print "flooding the first packet"
           self.send_packet(packet_in.buffer_id, packet_in.data,
                       of.OFPP_FLOOD, packet_in.in_port)
           #self.matrix[(packet.src,packet.dst)]+=1      
           entry = SourcetoPort(src_address=str(packet.src) , port_no=packet_in.in_port)
           #add the record to the session object
           session.add(entry)
           #add the record to the session object
           session.commit()

我有这段代码。我替换了

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

if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar() is not None:

Now I am getting the following error.


  File "/home/karthik/pox/tutorial.py", line 86, in act_like_switch
    self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, packet_in.in_port)
AttributeError: 'NoneType' object has no attribute 'port_no'
^CINFO:core:Going down...

上面这段代码曾经用于计数查询。为什么现在它可以用于存在查询。

【问题讨论】:

  • 你有什么问题?
  • @ChrisBain 我已经更新了问题。
  • exists() 不返回 True 或 False ?

标签: python python-2.7 sqlalchemy


【解决方案1】:

正如 francis-avila 在他出色的 answer to your other question 中解释的那样,exists()...scalar() is not None 存在逻辑问题。它返回 True 或 False - 所以它总是不返回 None。抱歉,昨天在 SQLAlchemy 中建议如何使用存在时是我的错误。

您的代码是正确的,并且在更改使用exists() 查询结果的逻辑后应该可以工作:

if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar() is not None:

if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar():

here 描述的使用 exists()...one() 和处理异常的方法也可以。请记住,在 python 中处理异常总是更昂贵的操作(使用更多的 cpu 周期),然后只是进行条件检查。如果您的应用程序不是性能关键 - 使用 try/catch 异常处理就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-23
    • 2021-12-15
    • 1970-01-01
    • 2019-07-22
    • 2011-11-30
    • 2011-06-19
    • 1970-01-01
    • 2017-04-25
    相关资源
    最近更新 更多