Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何以正确的方式连接到Python中的MySQL数据库?_Python_Mysql_Database_Peewee - Fatal编程技术网

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

如何以正确的方式连接到Python中的MySQL数据库?,python,mysql,database,peewee,Python,Mysql,Database,Peewee,更新因此,对于这里两位伟大用户的友好和有用的回复,我做了以下几点 你好亲爱的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

更新因此,对于这里两位伟大用户的友好和有用的回复,我做了以下几点


你好亲爱的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
martin@linux-70ce:~/perl>python cpan_100.py
回溯(最近一次呼叫最后一次):
文件“cpan_100.py”,第45行,在
user=user.create(name=entry[“name”],cname=entry[“cname”],
TypeError:字符串索引必须是整数,而不是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()
导入urllib
导入URL解析
进口稀土
#进口皮维
导入json
从peewee进口*
#从peewee导入MySQLDatabase('cpan',user='root',passwd='rimbaud')
db=MySQLDatabase('cpan',user='root',passwd='rimbaud')
类别用户(型号):
name=TextField()
cname=TextField()
email=TextField()
url=TextField()
类元:
database=db#此模型使用cpan数据库
User.create_table()#确保已创建表
url=”http://search.cpan.org/author/?W"
html=urllib.urlopen(url.read())
对于lk、capname、re.findall(“
(.*?”,html)中的名称: alk=urlparse.urljoin(url,lk) 数据={'url':alk,'name':name,'cname':capname} phtml=urllib.urlopen(alk.read)() memail=重新搜索(“”,phtml) 如果是memail: 数据['email']=memail.group(1) #data=json.load('email')#这里是您的json数据文件 对于数据中的条目:#假设您的数据是一个JSON对象数组 user=user.create(name=entry[“name”],cname=entry[“cname”], 电子邮件=条目[“电子邮件”],url=条目[“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>
martin@linux-70ce:~/perl>python cpan_100.py
回溯(最近一次呼叫最后一次):
文件“cpan_100.py”,第27行,在
User.create_table()#确保已创建表
文件“build/bdist.linux-i686/egg/peewee.py”,第3078行,在create_表中
文件“build/bdist.linux-i686/egg/peewee.py”,第2471行,在create_表中
执行sql中的文件“build/bdist.linux-i686/egg/peewee.py”,第2414行
文件“build/bdist.linux-i686/egg/peewee.py”,第2283行,在
执行sql中的文件“build/bdist.linux-i686/egg/peewee.py”,第2406行
文件“/usr/lib/python2.7/site packages/MySQLdb/cursors.py”,执行中的第174行
errorhandler(self、exc、value)
文件“/usr/lib/python2.7/site packages/MySQLdb/connections.py”,第36行,在defaulterrorhandler中
提高errorclass,errorvalue
peewee.OperationalError:(1050,“表‘用户’已存在”)
martin@linux-70ce:~/perl>
如果你能帮助我,我将非常高兴!谢谢你的一切帮助


问候语

它似乎正确地连接到了您的数据库,但由于以下原因而失败:

User.create_table() #ensure table is created
尝试创建表失败,因为表已存在,因此出现错误消息:

peewee.OperationalError: (1050, "Table 'user' already exists") 
试着评论一下:

#User.create_table()

首先,我应该说我对
peewee
一无所知。但是这条消息
peewee.OperationalError:(1050,“表'user'已经存在”)
似乎是您问题的核心:您正在无条件地使用
user.create\u Table()创建表
然而,你应该检查它是否存在,并且只有当它不存在时才尝试创建它。您好,亲爱的isedev。首先非常感谢您的提示。我将进行检查,并将仔细查看数据库中的allready激励表。我猜我必须删除一些东西,例如数据库中的表-以便让它运行…他说llo我发布了一个更新-请参阅原始帖子的顶部。我很想听到你的消息。我想我们非常接近你好亲爱的bryn非常感谢我删除了db cpan并再次运行程序查看结果:
martin@linux-70ce:~/perl>python-cpan\u 100.py回溯(最后一次调用):文件“cpan\u 100.py”,第45行,在user=user.create(name=entry[“name”],cname=entry[“cname”],TypeError:string索引必须是整数,而不是str
hello我发布了一个更新-请看原始帖子的顶部。我很想听到你的消息。我认为我们非常接近