2016-11-09 79 views
0

我的目標是從Excel工作表中讀取數據並在SQL服務器上創建數據庫。我正在嘗試使用SQLalchemy編寫示例代碼,而我對它很陌生。我至今的代碼是:Autoflush錯誤和filter_by()查詢給出意想不到的結果

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子句)它給<__main__.blc object at 0x00705770>錯誤 後來,我打算有插入子句放在一個循環中,它將從Excel工作表讀取數據。

回答

0

結果是一個對象引用類BLC。要獲得所需的專欄,我必須做result.ColName

相關問題