如何以正确的方式在Python中连接MySQL数据库?

时间:2014-09-07 16:44:22

标签: python mysql database peewee

更新; 因此,我在这里做了以下两位优秀用户的善意和有用的回复

亲爱的bryn亲爱的bryn非常感谢我删除了db cpan并再次运行程序看到结果:

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

这有点困难 - 为什么我会得到这些结果!!?

这里是原始帖子

对于python和编程来说都是新手。

我正在尝试使用peewee连接到亚马逊RDS上的MySQL数据库,我无法让它工作。我是数据库的新手,所以我可能做了一些愚蠢的事情,但这就是我正在尝试的:我试图在peewee的python中连接数据库,但在某个时刻程序失败。

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()

我得到以下结果

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>
如果你能帮助我,我会非常高兴的!感谢您的帮助

问候

1 个答案:

答案 0 :(得分:1)

看起来它正确连接到您的数据库,但因为行<:p>而失败

User.create_table() #ensure table is created

尝试创建表然后失败,因为表已经存在,因此出现错误消息:

peewee.OperationalError: (1050, "Table 'user' already exists") 

尝试将其评论出来:

#User.create_table()