Suraj Nagabhushana Suraj Nagabhushana - 1 year ago 48
Python Question

Autoflush error and filter_by() query giving unexpected result

My goal is to read data off of an excel sheet and create a database on a SQL server. I am trying to write a sample code using SQLalchemy and I am new to it. The code that I have so far is:

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)


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)



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

When I am performing a filter query (which I am assuming it is similar to where clause in SQL) it gives
<__main__.blc object at 0x00705770>
Eventually, I plan to have the insert clause on a loop and it will read data from an excel sheet.

Answer Source

Result is an object that references the class blc. To get the desired column, I had to do result.ColName.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download