0

我想使用 pylast 接口将 last.fm 的用户最近的音乐曲目列表保存到 postgresql 数据库表中。但是当我尝试向表中插入值时,它会显示错误。代码示例:

import pylast
import psycopg2
import re
from md5 import md5
import sys
import codecs
import psycopg2.extensions

psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
user_name = raw_input("Enter last.fm username: ")
user_password = raw_input("Enter last.fm password: ")

api_key = '*********'
api_secret = '********'

#Lastfm network authentication
md5_user_password = md5(user_password).hexdigest()
network = pylast.get_lastfm_network(api_key, api_secret,user_name,md5_user_password)

used=pylast.User(user_name, network)

recent_tracks=used.get_recent_tracks(10)

# Database connection 
try:
    conn=psycopg2.connect("dbname='**' user='postgres' host='localhost'   password='*'")
    conn.set_client_encoding('UNICODE')
except:
    print "I am unable to connect to the database, exiting."
    sys.exit()
cur=conn.cursor()

for i, artist in enumerate(recent_tracks):
    for key in sorted(artist):

        cur.execute("""
        INSERT INTO u_recent_track(Playback_date,Time_stamp,Track) 
        VALUES (%s,%s,%s)""",  (key, artist[key]))

    conn.commit()
cur.execute("SELECT * FROM u_recent_track;")
cur.fetchone()
for row in cur:
    print '   '.join(row[1:])

cur.close()
conn.close()

这里“recent_tracks”元组的值例如:

artist 0
  - playback_date : 5 May 2010, 11:14
  - timestamp : 1273058099
  - track         : Brian Eno - Web

我想将这些值存储在 u_recent_track(Tid, Playback_date, Time_stamp, Track) 下。有人知道如何解决这个问题吗?当我尝试运行时,它显示错误:

Traceback (most recent call last):
  File "F:\JavaWorkspace\Test\src\recent_track_database.py", line 50, in <module>
    VALUES (%s,%s,%s)""",  (key, artist[key]))
IndexError: tuple index out of range
4

3 回答 3

1

sorted(artist)返回艺术家的有序列表,当您对其进行迭代时,它会返回artist. 因此,当您尝试访问artist[key]它时,实际上是在尝试访问artist由索引索引的元素,该元素artist本身就是一个元素。元组不能以这种方式工作。

看来您使用的是 python2.5 或更低版本,因此您可以这样做:

cur.executemany("""
        INSERT INTO u_recent_track(Playback_date,Time_stamp,Track) 
        VALUES (%(playback_date)s,%(timestamp)s,%(track)s)""",  recent_tracks)
conn.commit()

这应该有效。

于 2010-05-06T11:18:17.623 回答
0

(Playback_date,Time_stamp,Track) indicates you want to insert three values into a row. VALUES (%s,%s) should therefore be VALUES (%s,%s,%s) and (key, artist[key]) should be a tuple with 3 elements, not 2.

Try:

for track in recent_tracks:
    cur.execute("""
        INSERT INTO u_recent_track(Playback_date,Time_stamp,Track) 
        VALUES (%s,%s,%s)""",  (track.get_date(), track.get_timestamp(), track.get_track()))
    conn.commit()

PS. This is where I'm getting my information about the pylast API.

PPS. If my reading of the documentation is correct, track.get_track() will return a Track object. It has methods like get_album, get_artist, get_id and get_title. Exactly what do you want stored in the Track column of the u_recent_track database table?

于 2010-05-06T11:14:47.330 回答
0

此错误与 Postgres 无关,而是与artist变量有关。你首先说:

for key in sorted(artist):

暗示它是一个列表,但是您正在访问它,就好像它是一本字典一样,这会引发错误。它是哪一个?你能展示一个完整内容的例子吗?

于 2010-05-06T11:18:31.117 回答