似乎无法获得Python,Peewee,pymysql& MySql数据库没有工作' returns_clause'错误

时间:2018-04-17 07:43:24

标签: python mysql python-3.x peewee pymysql

我对Python很陌生并试图使用一个非常简单的表DB。

我使用的是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 "中收到以下错误:线。我搜索了“return_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:\.dev\Client\Cropnosis\Researcher\lib\site-packages\peewee.py", line 5161, in create
    inst.save(force_insert=True)
  File "C:\.dev\Client\Cropnosis\Researcher\lib\site-packages\peewee.py", line 5284, in save
    pk_from_cursor = self.insert(**field_dict).execute()
  File "C:\.dev\Client\Cropnosis\Researcher\lib\site-packages\peewee.py", line 5128, in insert
    return ModelInsert(cls, cls._normalize_data(__data, insert))
  File "C:\.dev\Client\Cropnosis\Researcher\lib\site-packages\peewee.py", line 5868, in __init__
    if self.model._meta.database.returning_clause:
AttributeError: 'Connection' object has no attribute 'returning_clause'

1 个答案:

答案 0 :(得分:1)

你想要这个:

from peewee import *

# this is peewee.MySQLDatabase, not pymysql's connection class.
db = MySQLDatabase('the_db_name', user='xxxxx', passwd='xxxxx')