update; hence to the kind and helpful replies of two great users here i did the following
hello dear bryn many thanks i erased the db cpan and runned the programme again see the results:
martin@linux-70ce:~/perl> python cpan_100.py
Traceback (most recent call last):
File "cpan_100.py", line 45, in <module>
user = User.create(name=entry["name"], cname=entry["cname"],
TypeError: string indices must be integers, not str
well this is somewhat difficult - why do i get these results!!?`
here the original posting
fairly new to python and to programming too.
I'm trying to connect to a MySQL database on Amazon's RDS using peewee and I can't get it to work. I'm new to databases so I'm probably doing something stupid, but this is what I'm trying: well i tried to get connection to a database in python with peewee but at a certain point the programme fails.
import urllib
import urlparse
import re
# import peewee
import json
from peewee import *
#from peewee import MySQLDatabase ('cpan', user='root',passwd='rimbaud')
db = MySQLDatabase('cpan', user='root',passwd='rimbaud')
class User(Model):
name = TextField()
cname = TextField()
email = TextField()
url = TextField()
class Meta:
database = db # this model uses the cpan database
User.create_table() #ensure table is created
url = "http://search.cpan.org/author/?W"
html = urllib.urlopen(url).read()
for lk, capname, name in re.findall('<a href="(/~.*?/)"><b>(.*?)</b></a><br/><small>(.*?)</small>', html):
alk = urlparse.urljoin(url, lk)
data = { 'url':alk, 'name':name, 'cname':capname }
phtml = urllib.urlopen(alk).read()
memail = re.search('<a href="mailto:(.*?)">', phtml)
if memail:
data['email'] = memail.group(1)
# data = json.load('email') #your json data file here
for entry in data: #assuming your data is an array of JSON objects
user = User.create(name=entry["name"], cname=entry["cname"],
email=entry["email"], url=entry["url"])
user.save()
i get back the following results
martin@linux-70ce:~/perl> python cpan_100.py
Traceback (most recent call last):
File "cpan_100.py", line 27, in <module>
User.create_table() #ensure table is created
File "build/bdist.linux-i686/egg/peewee.py", line 3078, in create_table
File "build/bdist.linux-i686/egg/peewee.py", line 2471, in create_table
File "build/bdist.linux-i686/egg/peewee.py", line 2414, in execute_sql
File "build/bdist.linux-i686/egg/peewee.py", line 2283, in __exit__
File "build/bdist.linux-i686/egg/peewee.py", line 2406, in execute_sql
File "/usr/lib/python2.7/site-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
peewee.OperationalError: (1050, "Table 'user' already exists")
martin@linux-70ce:~/perl>
if you can help me i would e very glad! Thanks for any and all help
greetings
It looks like it connects to your database properly but fails because the line:
User.create_table() #ensure table is created
tries to create a table then fails because a table already exists, hence the error message:
peewee.OperationalError: (1050, "Table 'user' already exists")
Try commenting it out:
#User.create_table()