【问题标题】:Display SQL JOINED table in Python在 Python 中显示 SQL JOINED 表
【发布时间】:2020-12-31 11:44:30
【问题描述】:

我在下面的代码中添加了LIMIT 3 以显示 JOINED 表的 3 个结果。但是在 Python 中显示了多行。在SQL执行中,没问题。

请指教是什么原因?谢谢

lib = cur.execute('''SELECT Track.title, Artist.name, Album.title, Genre.name
  FROM Track JOIN Genre JOIN Album JOIN Artist
  ON Track.genre_id = Genre.ID AND Track.album_id = Album.id
  AND Album.artist_id = Artist.id
  ORDER BY Artist.name  LIMIT 3''')
for row in lib:
  print(row)

【问题讨论】:

  • 检查PEP-249 DB-API 2.0 中的cursor 对象:execute(operation [, parameters]):返回值未定义。。您应该删除limit 并改用fetchmany(3)

标签: python sql sqlite


【解决方案1】:

尝试将其更改为:

 cur.execute('''SELECT Track.title, Artist.name, Album.title, Genre.name
  FROM Track JOIN Genre JOIN Album JOIN Artist
  ON Track.genre_id = Genre.ID AND Track.album_id = Album.id
  AND Album.artist_id = Artist.id
  ORDER BY Artist.name  LIMIT 3''')
lib = cur.fetchall()

【讨论】:

  • 您好,显示的结果还是多行。不是 3 行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多