21.12.2022 Views

python_para_desenvolvedores_2ed

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

MVC 187

# Conecta ao bando

db = sql.create_engine('sqlite:///zoo.db')

# Acesso aos metadados

metadata = sql.MetaData(db)

try:

# Carrega metadados da tabela

zoo = sql.Table('zoo', metadata, autoload=True)

except:

# Define a estrutura da tabela zoo

zoo = sql.Table('zoo', metadata,

sql.Column('id', sql.Integer, primary_key=True),

sql.Column('nome', sql.String(100),

unique=True, nullable=False),

sql.Column('quantidade', sql.Integer, default=1),

sql.Column('obs', sql.String(200), default='')

)

# Cria a tabela

zoo.create()

# Os nomes das colunas

colunas = [col for col in zoo.columns.keys()]

colunas.remove('id')

class Root(object):

"""Raiz do site"""

@cherrypy.expose

def index(self, **args):

"""

Lista os registros

"""

msg = ''

op = args.get('op')

ident = int(args.get('ident', 0))

novo = {}

for coluna in colunas:

novo[coluna] = args.get(coluna)

if op == 'rem':

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!