Python for循环“过早”退出

2024-09-27 19:30:51 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑: 答:我需要为“einlesen()”函数创建另一个游标。在

这是我第一次在Python中使用SQLite3,请原谅我(可能)糟糕的语法;) 我试图建立一个DVD数据库,直接从amazon获取必要的信息(演员等)。整个程序基于SQLite3和python2.7。在

一切都很好,除了我的plannes“更新”功能。在

def update():
    print 'Update Datenbank....bitte warten....'
    cursor.execute('''SELECT titel, amazon, erhalten, ausgeliehen FROM movies''')
    antwort = 'update'
    for row in cursor:
        stelle = row[1]
        ausg = row[2]
        erh = row[3]
        einlesen(stelle, ausg, erh, antwort)
        print row[0]
    raw_input('Update komplett!')
    menu()

问题是,循环在一次迭代后退出。在

输出如下:

^{pr2}$

所以我知道,循环和函数调用是正确的(数据库得到正确的更新-由函数'einlesen()'完成),但是还有更多的迭代,而不仅仅是一个。。。 所以我的问题是:怎么了?;)

以下是(缩写)“einlesen()”函数:

def einlesen(asin, ausg, erh, antwort):
    d = {}
    infos = urllib.urlopen('http://www.amazon.de/dp/'+asin).read()
    titel = infos[infos.find('Kaufen Sie')+11:infos.find('nstig ein')-3]
    art = 'dvd'
    infos = remove_html_tags(infos)
    infos = infos[infos.find('Darsteller: '):infos.find('Durchschnittliche')]
    infos = infos.split('\n')
    for x in range(200):
        try:
            infos.remove('')
        except:
            break
    for element in infos:
            d[element.split(': ')[0].lstrip()] = element.split(': ')[1]

#(excluded the whole Info-Scraping process)

    if antwort == 'update':
        movie = dauer, art, regie, jahr, fsk, darsteller, titel
        sql = ('''UPDATE movies SET laufzeit = ?, art = ?, regie = ?, jahr = ?, fsk =     ?, darsteller = ? WHERE titel = ?''')
        cursor.execute(sql, movie)
        connection.commit()
    else:
        menu()

谢谢你的帮助。在


Tags: 函数inamazonforupdatefindcursorrow
1条回答
网友
1楼 · 发布于 2024-09-27 19:30:51

当您仍在循环SELECT的结果时执行UPDATE。这将删除第一个cursor.execute()的结果。在

使用第二个光标。在

编辑:

cur1 = con.cursor()
cur2 = con.cursor()

cur1.execute("SELECT ...")
for row in cur1:
    cur2.execute("UPDATE ...")

相关问题 更多 >

    热门问题