为什么我的 sqlite 查询不打印所有数据?
Why does my sqlite query not print all data?
我注意到我的查询正在从我的联接表中获取所有数据,但我只能在我专门尝试访问这些数据时读取它。
我未编辑的文件:
query = db.session.query(Rating, Song).filter(Rating.id==Song.id).all()
print(query) #<----- This prints [(4.75, MMMBop), (3.00, bombastic)]
for x in query:
print(f"{x[1].title}:{x[1].artist}:{x[1].released}") #<-- This prints MMMBop:Hansons:1997\nbombastic:shaggy:1995
这是为什么?
编辑
我已经添加了我的模型。 repr 是我检查的第一件事,我在重新启动后再次获得了 运行 代码,因此不会有任何潜伏的变量。没有代表甚至包括艺术家和发行。
from application import db
association_table = db.Table('association',
db.Column('songs_id', db.Integer,
db.ForeignKey('songs.id')),
db.Column('genres_id', db.Integer,
db.ForeignKey('genres.id'))
)
class Rating(db.Model):
__tablename__ = 'songs_ratings'
id = db.Column(db.Integer, primary_key=True)
rating = db.Column(db.Numeric(precision=3, scale=2),
index=True, nullable=False)
def __repr__(self):
return '{}'.format(self.rating)
class Song(db.Model):
__tablename__ = 'songs'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), index=True, unique=True, nullable=False)
artist = db.Column(db.String(30), primary_key=False,
unique=False, nullable=False)
release = db.Column(db.Date, nullable=False)
genres = db.relationship(
"Genre", secondary=association_table, backref=db.backref('songs'))
def __repr__(self):
return '{}'.format(self.title)
class Genre(db.Model):
__tablename__ = 'genres'
id = db.Column(db.Integer, primary_key=True)
category = db.Column(db.String(80), index=True,
unique=True, nullable=False)
def __repr__(self):
return '{}'.format(self.category)
您从 print(query)
看到的输出只是 query
对象的字符串表示形式。它不会为您提供有关如何访问基础数据的任何信息。我建议您阅读更多的 sqlalchemy 教程和文档,以了解如何正确使用 db.session.query()
的结果。在这种情况下,您需要获取所有行作为游标:
for x in query.fetchall():
的确,你查询了returns所有的数据(从发出的SQL语句中大概可以看出)。
问题不是您不能“看到”数据,而是当您使用“打印”语句时看到的是什么。
query = db.session.query(Rating, Song).filter(Rating.id==Song.id).all()
print(query) #<----- This prints [(4.75, MMMBop), (3.00, bombastic)]
上面之所以打印你看到的是因为Rating
和Song
都有__repr__
方法,分别只打印rating
和title
。
但是当你执行下面的打印语句时,你正在决定要打印什么:
for x in query:
print(f"{x[1].title}:{x[1].artist}:{x[1].released}") #<-- This prints MMMBop:Hansons:1997\nbombastic:shaggy:1995
我的建议 是让您实施更详细的 Song.__repr__
方法以包含您关心的所有列。
我注意到我的查询正在从我的联接表中获取所有数据,但我只能在我专门尝试访问这些数据时读取它。
我未编辑的文件:
query = db.session.query(Rating, Song).filter(Rating.id==Song.id).all()
print(query) #<----- This prints [(4.75, MMMBop), (3.00, bombastic)]
for x in query:
print(f"{x[1].title}:{x[1].artist}:{x[1].released}") #<-- This prints MMMBop:Hansons:1997\nbombastic:shaggy:1995
这是为什么?
编辑
我已经添加了我的模型。 repr 是我检查的第一件事,我在重新启动后再次获得了 运行 代码,因此不会有任何潜伏的变量。没有代表甚至包括艺术家和发行。
from application import db
association_table = db.Table('association',
db.Column('songs_id', db.Integer,
db.ForeignKey('songs.id')),
db.Column('genres_id', db.Integer,
db.ForeignKey('genres.id'))
)
class Rating(db.Model):
__tablename__ = 'songs_ratings'
id = db.Column(db.Integer, primary_key=True)
rating = db.Column(db.Numeric(precision=3, scale=2),
index=True, nullable=False)
def __repr__(self):
return '{}'.format(self.rating)
class Song(db.Model):
__tablename__ = 'songs'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), index=True, unique=True, nullable=False)
artist = db.Column(db.String(30), primary_key=False,
unique=False, nullable=False)
release = db.Column(db.Date, nullable=False)
genres = db.relationship(
"Genre", secondary=association_table, backref=db.backref('songs'))
def __repr__(self):
return '{}'.format(self.title)
class Genre(db.Model):
__tablename__ = 'genres'
id = db.Column(db.Integer, primary_key=True)
category = db.Column(db.String(80), index=True,
unique=True, nullable=False)
def __repr__(self):
return '{}'.format(self.category)
您从 print(query)
看到的输出只是 query
对象的字符串表示形式。它不会为您提供有关如何访问基础数据的任何信息。我建议您阅读更多的 sqlalchemy 教程和文档,以了解如何正确使用 db.session.query()
的结果。在这种情况下,您需要获取所有行作为游标:
for x in query.fetchall():
的确,你查询了returns所有的数据(从发出的SQL语句中大概可以看出)。
问题不是您不能“看到”数据,而是当您使用“打印”语句时看到的是什么。
query = db.session.query(Rating, Song).filter(Rating.id==Song.id).all()
print(query) #<----- This prints [(4.75, MMMBop), (3.00, bombastic)]
上面之所以打印你看到的是因为Rating
和Song
都有__repr__
方法,分别只打印rating
和title
。
但是当你执行下面的打印语句时,你正在决定要打印什么:
for x in query:
print(f"{x[1].title}:{x[1].artist}:{x[1].released}") #<-- This prints MMMBop:Hansons:1997\nbombastic:shaggy:1995
我的建议 是让您实施更详细的 Song.__repr__
方法以包含您关心的所有列。