EDIT:
ANSWER: I needed to create another cursor for the ‘einlesen()’ function.
This is my first time using SQLite3 in Python, so excuse my (maybe) horrible syntax 😉
I’m trying to build kind of a DVD-Database, which fetches necessary information (actors etc.) directly from amazon. The whole program is based upon SQLite3 and Python 2.7.
Everything works well, except my plannes “update” feature.
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()
The problem is, the loop exits after one iteration.
The output looks like this:
Update Datenbank....bitte warten....
#a few seconds pass
The Day After Tommorrow
Update komplett!
So I see, the loop and the function call are correct (the database gets updated correctly – done by the function ‘einlesen()’), but there are more iterations, not just one…
So my question is: What is wrong? 😉
Here is the (abbreviated) ‘einlesen()’ function:
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()
Thanks for your help.
Your execute an
UPDATEwhile you are still looping over the result of theSELECT. This drops the result of the firstcursor.execute().Use a second cursor.
Edit: