Autoflush错误和filter_by()查询给出意外结果

时间:2016-11-09 00:10:05

标签: python sqlalchemy

我的目标是从Excel工作表中读取数据并在SQL服务器上创建数据库。我正在尝试使用SQLalchemy编写示例代码,我是新手。我到目前为止的代码是:

import time
from sqlalchemy import create_engine, Column, Integer, Date, String, Table, MetaData,table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

engine = create_engine('sqlite:///:memory:', echo = False)

Base = declarative_base()

class blc(Base):
    __tablename__ = 'BLC_Databse'

    date = Column(String, primary_key = True)
    RES = Column(String)
    BTTLCOLUMN = Column(String)
    CS_HR = Column(Integer)

Base.metadata.create_all(engine)

sample = blc(date=time.strftime("%m/%d/%y") , RES = 'BDY_21', BTTLCOLUMN = '2075', CS_HR = 563) 

Session = sessionmaker(bind=engine)
session = Session()

sample2 = blc(date=time.strftime("%m/%d/%y") , RES = 'BDY_21', BTTLCOLUMN = '2076', CS_HR = 375)

session.add(sample2)

session.commit()

with session.no_autoflush:
    result = session.query(blc).filter_by(RES = 'BDY_21').first()
    print(result)

当我执行过滤器查询(我假设它类似于SQL中的where子句)时,它会出现<__main__.blc object at 0x00705770>错误 最后,我计划在循环中使用insert子句,它将从excel表中读取数据。

1 个答案:

答案 0 :(得分:0)

Result是一个引用类blc的对象。要获得所需的列,我必须执行result.ColName