Python 如何通过指定模式从postgresql数据库中获取行?
我是Postgresql和Sqlalchemy的新手。我有以下文件layout.py。在本文中,我在“col”模式下创建了两个表名“layout”和“layout default” 插入数据:Python 如何通过指定模式从postgresql数据库中获取行?,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,我是Postgresql和Sqlalchemy的新手。我有以下文件layout.py。在本文中,我在“col”模式下创建了两个表名“layout”和“layout default” 插入数据: INSERT INTO col.layout(layout_id,name,layout,grid_id,user_id,ispublic,issystemlayout,isactive,createdby,createdat, modifiedat,modifiedby) VALUES('ba0233d
INSERT INTO col.layout(layout_id,name,layout,grid_id,user_id,ispublic,issystemlayout,isactive,createdby,createdat, modifiedat,modifiedby) VALUES('ba0233d7-d917-4303-b4bf-c2544a617d33','Layout1','{"Name":"Manish","Place":"Pune"}',1,12345,'1','0','1','201819','2015/05/20','2015/05/16',123);
获取数据:
Session = sessionmaker(bind=db)
session = Session()
Base.metadata.create_all(db)
session.query("SET search_path TO col;")
result = []
selected_columns = Layout.__table__.columns
print("Selected columns {}".format(selected_columns))
record = session.query(Layout).with_entities(*selected_columns).all()
for row in record:
print(row)
result.append(row)
print(json.dumps(result))
session.close()
但它并没有在“col”模式下显示数据。请建议,我该怎么办
postgres数据库的模式可以作为元组或dict通过table_args属性传递
因此,对于您的问题,您的类定义应该具有以下额外属性:
class Layout(Base):
__tablename__ = "layout"
__table_args__ = {"schema": "col"}
layout_id = Column(UUID(as_uuid=True), nullable=False, primary_key=True)
name = Column(String(1000), nullable=False)
layout = Column(String(10000), nullable=False)
grid_id = Column(Integer, nullable=False)
user_id = Column(Integer, nullable=False)
issystemlayout = Column(Integer, default=0, nullable=False)
ispublic = Column(Integer, default=0, nullable=False)
isactive = Column(Integer, default=0, nullable=False)
createdby = Column(Integer, default=1, nullable=False)
createdat = Column(TIMESTAMP, default=(datetime.datetime.now()), nullable=False)
modifiedat = Column(TIMESTAMP, default=(datetime.datetime.now()), nullable=False)
modifiedby = Column(Integer, default=1, nullable=False)
尝试使用属性<代码>\uuuu table\u args\uuuuu={“schema”:“col”}和
\uuuu tablename\uuuu=“layout”
@NihalSangeeth在您的类定义中的哪一行。还需要一个小帮助。当我尝试运行上述脚本时,它会说“UUID类型的对象不可JSON序列化”。如果您对此有任何想法,请告诉我。打印(行)
返回什么?print(result)返回什么?这一行:“print(json.dumps(result))”您的layout_id
列是一个UUID对象。这是不可序列化的。也许您需要UUID对象的字符串值。您可以使用\uuu str\uuu()
或仅使用str()
class Layout(Base):
__tablename__ = "layout"
__table_args__ = {"schema": "col"}
layout_id = Column(UUID(as_uuid=True), nullable=False, primary_key=True)
name = Column(String(1000), nullable=False)
layout = Column(String(10000), nullable=False)
grid_id = Column(Integer, nullable=False)
user_id = Column(Integer, nullable=False)
issystemlayout = Column(Integer, default=0, nullable=False)
ispublic = Column(Integer, default=0, nullable=False)
isactive = Column(Integer, default=0, nullable=False)
createdby = Column(Integer, default=1, nullable=False)
createdat = Column(TIMESTAMP, default=(datetime.datetime.now()), nullable=False)
modifiedat = Column(TIMESTAMP, default=(datetime.datetime.now()), nullable=False)
modifiedby = Column(Integer, default=1, nullable=False)