【问题标题】:Specifying Table to Connect to with Python Peewee使用 Python Peewee 指定要连接的表
【发布时间】:2015-02-18 13:03:09
【问题描述】:

我已经阅读了Peewee MySQL API documentationthis question;但是,我不明白的是如何使用 Peewee 连接到数据库中的指定表。基本上我要做的就是连接到名为as_schema 的数据库中名为Persons 的表,设置某种基本的对象关系映射,并打印出所有条目的aNum 列值。

我试图读取的表 Persons 具有以下列:

  • varchar 称为 aNum
  • bool 称为access
  • bool 称为ajar
  • bool 称为ebr
  • 日期调用weekof

我的代码包含以下内容:

import peewee
from peewee import *

db = MySQLDatabase('as_schema', user='root',passwd='')#this connection path works perfectly, tried it using the standard MySQLdb.connect

class Person(Model):
    class Meta:
        database = db

class User(Person):
    aNum = CharField()

Person.create_table()
person = User(aNum = 'a549189')
person.save();

for person in Person:
    print person.aNum

我得到的错误是:

【问题讨论】:

标签: python mysql orm peewee


【解决方案1】:
class Person(Model):
    class Meta:
        database = db
        db_table = 'Persons'  # Add this.

在文档中,您可以找到支持的元选项列表:

http://docs.peewee-orm.com/en/latest/peewee/models.html#model-options-and-table-metadata

【讨论】:

    猜你喜欢
    • 2014-03-27
    • 2020-11-18
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多