简体   繁体   中英

Specifying Table to Connect to with Python Peewee

I have read the Peewee MySQL API documentation and this question ; however, what I do not understand is how to connect to a specified table in a db using Peewee. Essentially all I'm trying to do is connect to aa table called Persons in a db called as_schema , set up some sort of basic object-relational mapping, and print out all entries' aNum column values.

My table Persons that I'm trying to read from has the following columns:

  • varchar called aNum
  • bool called access
  • bool called ajar
  • bool called ebr
  • date called weekof

My code consists of the following:

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

The error I'm getting is: 在此处输入图片说明

class Person(Model):
    class Meta:
        database = db
        db_table = 'Persons'  # Add this.

In the docs, you can find a list of supported meta options:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM