似乎无法使python、peewee、pymysql和mysql数据库正常工作没有“returning\u子句”错误

elcex8rz  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(361)

我对python非常陌生,尝试使用一个非常简单的单表db。
我正在使用python3、peewee和pymysql。数据库是mysql,在我的windows10pc(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\u 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'
busg9geu

busg9geu1#

你想要这个:

from peewee import *

# this is peewee.MySQLDatabase, not pymysql's connection class.

db = MySQLDatabase('the_db_name', user='xxxxx', passwd='xxxxx')

相关问题