简体   繁体   English

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

[英]How do I connect to a MySQL Database in Python the right way?

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: 你好,亲爱的布莱恩,非常感谢,我删除了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

well this is somewhat difficult - why do i get these results!!?` 好吧,这有点困难-为什么我得到这些结果!

here the original posting 这里是原始帖子

fairly new to python and to programming too. 对python和编程来说还相当新。

I'm trying to connect to a MySQL database on Amazon's RDS using peewee and I can't get it to work. 我正在尝试使用peewee连接到Amazon RDS上的MySQL数据库,但无法正常工作。 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. 我是数据库新手,所以我可能正在做一些愚蠢的事情,但这就是我正在尝试的方法:好吧,我尝试使用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()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM