Sql 如何将文件元数据插入数据库(例如,对于.mp3文件,插入其名称、相册、艺术家等)

Sql 如何将文件元数据插入数据库(例如,对于.mp3文件,插入其名称、相册、艺术家等),sql,database,postgresql,Sql,Database,Postgresql,我有一个包含mp3文件的文件夹,其中包含标题、姓名、艺术家和专辑。有没有办法使用PostgreSQL在具有相同列的表中插入这些详细信息?或者将数据转换成csv/txt文件,然后导入?我不需要文件作为blob,只需要元数据 安装 确保您的PostgreSQL数据库功能正常并可供使用 确定如何解析MP3文件名 将解析逻辑替换为下面的脚本 似乎是脚本语言的理想用例。你熟悉吗?是的,我过去用过一点Python。 import os import psycopg2 con = psycopg2.con

我有一个包含mp3文件的文件夹,其中包含标题、姓名、艺术家和专辑。有没有办法使用PostgreSQL在具有相同列的表中插入这些详细信息?或者将数据转换成csv/txt文件,然后导入?我不需要文件作为blob,只需要元数据

  • 安装
  • 确保您的PostgreSQL数据库功能正常并可供使用
  • 确定如何解析MP3文件名
  • 将解析逻辑替换为下面的脚本

  • 似乎是脚本语言的理想用例。你熟悉吗?是的,我过去用过一点Python。
    import os 
    import psycopg2
    
    con = psycopg2.connect('postgres://your_username:your_password@localhost:5432/your_database_name')
    cur = con.cursor()
    cur.execute('''create table mp3 (
                       id serial,
                       song_title text, 
                       song_name text, 
                       artist text, 
                       album text);''')
    con.commit()
    
    for a_file in os.listdir():
        file_name, file_ext = os.path.splitext(a_file)
        if file_ext == '.mp3'
            # substitute line below with your parsing logic
            title, name, artist, album = file_name.split()
            cur.execute('''insert into mp3 (song_title,song_name,artist,album) 
                           values (%s,%s,%s,%s);''',(title,name,artist,album,))
            con.commit()
    
    cur.close()
    con.close()