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

标签 python mysql database peewee

更新;因此,对于两位优秀用户的友善和有用的回复,我做了以下操作

你好,亲爱的布林,非常感谢,我删除了 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 连接到 Amazon 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>

如果你能帮助我,我会很高兴!感谢您的帮助

问候

最佳答案

看起来它可以正确连接到您的数据库,但由于以下行而失败:

User.create_table() #ensure table is created

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

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

尝试将其注释掉:

#User.create_table()

关于python - 如何以正确的方式在 Python 中连接到 MySQL 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25712553/

相关文章:

python unittest因源代码中的相对导入而失败

python - 使用具有现有数据库 ID 字段的 Django 问题

mysql - 在 mysql 索引上优化 key_len

php - MySQL - 保存 JSON 时间戳

php - 如何在php中创建和更新文本文件以便可以显示最后发送的消息

java - DB2 和 Java。通过 GUI 将数据添加到数据库。

python - 将同一行中的多个值连接到一个列表中

python - Scrapy中是否可以通过CSS属性定位元素?

python - 只是好奇Python+Numpy来实时手势识别

mysql - 从学生和分数表中选择分数最高的学生