【问题标题】:How to only show certain words in a query如何仅在查询中显示某些单词
【发布时间】:2018-04-09 18:40:09
【问题描述】:

在我的测试程序中,用户可以保存播放列表,并且可以随意调用它们。例如,如果用户 James Smith 保存名为 My Playlist 的播放列表,则表的名称将为 James Smith_My Playlist。我将如何去做,所以当他们想要搜索所有 他们的 播放列表时,它 显示以 James Smith_ 开头的播放列表,但不显示他们的名字或下划线输出。

我的预期结果:

  • 用户保存播放列表
  • 播放列表名为FirstName LastName_NameOfPlaylist
  • 用户选择“查看所有播放列表 [表格]”
  • 输出显示以FirstName LastName_ 开头的表格,但在输出中仅显示NameOfPlaylist

我知道如何查询以某些字母/单词开头的表格,但我无法弄清楚如何输出它,没有FirstName LastName_

【问题讨论】:

  • 为什么不只拥有一个表playlists,并带有一个名为playlist_name 的字段?数据库的全部意义在于能够查询数据并通过特定参数进行过滤,而不是将数据分成不定数量的表。
  • 除了playlist_name,您还有user_first_nameuser_surname。或者只使用username,因为名称可能不适用于西方惯例。但是,原则是有一个包含允许您区分播放列表的字段的表。

标签: python sql python-3.x sqlite


【解决方案1】:

这不是建立数据库的方法;您不会为了获取特定结果而定义无限数量的表,所有表都包含相似的信息。相反,您应该将所有播放列表数据放入一个表中,但提供足够的字段来区分查询返回的值。一个例子(简化,它不是一个完整的解决方案!):

import sqlite3

def create_database():

    with sqlite3.connect('all_playlists.db') as conn:
        c = conn.cursor()
        c.execute("DROP TABLE playlists") # Gets messy if you re-run the code otherwise

        c.execute("""
                  CREATE TABLE IF NOT EXISTS playlists(
                      playlist_name TEXT,
                      username TEXT,
                      song_title TEXT)
                  """)


def add_playlist(playlist_name, username, songs):
    with sqlite3.connect('all_playlists.db') as conn:
        c = conn.cursor()
        for song in songs:
            c.execute("""
                      INSERT INTO playlists VALUES (?, ?, ?)
                      """, (playlist_name, username, song))


def get_all_playlists(username):
    with sqlite3.connect('all_playlists.db') as conn:
        c = conn.cursor()
        c.execute("""
                  SELECT DISTINCT playlist_name
                  FROM playlists
                  WHERE username = ?
                  """, (username,))
        return c.fetchall()


def get_songs_in_playlist(playlist, username):
    with sqlite3.connect('all_playlists.db') as conn:
        c = conn.cursor()
        c.execute("""
                  SELECT song_title
                  FROM playlists
                  WHERE playlist_name = ?
                  AND username = ?
                  """, (playlist, username))
        return c.fetchall()

if __name__ == '__main__':
    create_database()
    # Add some users and songs
    add_playlist('happy times', 'John Smith', ['Song A', 'Song B'])
    add_playlist('sad times', 'John Smith', ['Song C', 'Song D'])
    add_playlist('something else', 'John Smith', ['Song E', 'Song F', 
                                                  'Song G'])
    add_playlist('running out of ideas', 'Janet', ['Song F', 'Song G', 
                                                  'Song H'])

    # Get playlists of John Smith
    print("John Smith's playlists are: ", get_all_playlists('John Smith'))

    # Get Janet's
    print("Janet's playlists are: ", get_all_playlists('Janet'))

    # Get all songs in happy times
    print("All songs in happy times playlist by John are: ",
          get_songs_in_playlist('happy times', 'John Smith'))

如果您的应用程序变得足够大,您也不应该仅依靠名称来识别用户,因为您会遇到名称冲突。在这种情况下,您需要一个将用户映射到用户 ID 的登录系统,然后您可以使用它来连接表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2016-01-19
    • 2015-01-12
    相关资源
    最近更新 更多