我对Python很陌生,并尝试使用一个非常简单的单表数据库。
我正在使用python 3,peewee和pymysql。数据库是MySQL,在我的Windows 10 PC(wampserver(上本地,代码是;
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
import peewee
from peewee import *
db = MySQLdb.connect(user='xxxxx', passwd='xxxxx',
host='localhost', port=3306, db='xxxxx')
class SearchUrl(peewee.Model):
id = peewee.AutoField()
url = peewee.TextField()
page_title = peewee.TextField()
date_found = peewee.DateField()
date_last_changed = peewee.DateField()
article_status = peewee.CharField()
description = peewee.TextField()
class Meta:
database = db
newUrl = SearchUrl.create(url='http://cropnosis.co.uk', page_title="Cropnosis Ltd.",
date_found='2018-04-13', status='new', description='Cropnosis website')
newUrl.save()
for url in SearchUrl.filter(url='http://cropnosis.co.uk'):
print(url.description)
db.close()
每当我运行它时,我都会在">SearchUrl.create"行中收到以下错误。我已经搜索了"returning_clause"属性,但很少或根本没有提到它,尤其是关于MySQL。
任何帮助或相关链接非常感谢。
Traceback (most recent call last):
File "researcherDb.py", line 26, in <module>
date_found='2018-04-13', status='new', description='Cropnosis website')
File "C:.devClientCropnosisResearcherlibsite-packagespeewee.py", line 5161, in create
inst.save(force_insert=True)
File "C:.devClientCropnosisResearcherlibsite-packagespeewee.py", line 5284, in save
pk_from_cursor = self.insert(**field_dict).execute()
File "C:.devClientCropnosisResearcherlibsite-packagespeewee.py", line 5128, in insert
return ModelInsert(cls, cls._normalize_data(__data, insert))
File "C:.devClientCropnosisResearcherlibsite-packagespeewee.py", line 5868, in __init__
if self.model._meta.database.returning_clause:
AttributeError: 'Connection' object has no attribute 'returning_clause'
你想要这个:
from peewee import *
# this is peewee.MySQLDatabase, not pymysql's connection class.
db = MySQLDatabase('the_db_name', user='xxxxx', passwd='xxxxx')