Python, I want to know how to remove the table name when loading SQL data.

Asked 1 years ago, Updated 1 years ago, 107 views

The values of the table named Student currently in PostgreSQL (DB) using flasqlAlchemy in Python.session.They're calling me query.

When a value is called, the table name and attribute name are grouped together with '' (table name + attribute value (Student_id))

Can I just load the attribute value separately or remove the underbar? I looked it up because I thought there would be a setting to google and get only attribute values, but I haven't found it yet.<

I tried to remove the key value with .replace("__"", "), but it was tied to a tuple, so it couldn't be applied.

Database

Study [DB table name]
------------------------
[Property value]
id:1
name: Student 1
age:20

dbquery.py

records = db.session.execute(db.session.query(Student))
for row in records:
    result.append(dict(row))

print(result)

Results:[{'Student_id':1,'Student_name':'Student1','Student_age':20}]

Results I want

Results:[{'id':1,'name':'student1','age':20}] or [{'StudentId':1,'StudentName':'Student1','StudentAge':20}]

This is an addition to ==========================.============================

models.py

import datetime
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.ext.declarative import declarative_base

class Student(Base):
    __tablename__ = 'Student'
    name=Column(String(100), nullable=False)
    id = Column(Integer, primary_key=True, autoincrement=True)
    age = Column(Integer, default=0)

database.py

import os
from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import scoped_session, sessionmaker, joinedload
from models import Base

basedir = os.path.abspath(os.path.dirname(__file__))
engine = create_engine('postgresql+psycopg2://Project:0000@localhost/Project1',pool_size=150, max_overflow=130)

metadata = MetaData()
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))

def create_tables():
    Base.metadata.create_all(bind=engine)

_ _ init _ _.py

create_tables()

python orm flask postgresql

2022-09-22 18:43

2 Answers

records = db.session.execute(db.session.query(Student))

Please correct the above part as below.

records  = db.session.query(Student).all()

Please check as below.

records[0].__dict__


2022-09-22 18:43

Leave an additional answer.

Previously, when I loaded the table of postgreSQL from Python, it was made in ORM method, so information about the table was also included.
For example, 'Student_id' in the student table before the id.
I received the key and value as .items from dbquery.py and added it by updating it. I hope you'll find it useful.

result = list()
records = db.session.execute(db.session.query(Student))
for row in records:
   dict_value = dict()
   for x,y in row.items():
      dict_value.update({x:y})

   result.update(dict_value)

print(result)

Result: [{'id':1,'name':'Student1','age':20}]


2022-09-22 18:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.