I was playing around making a simple haiku site using sqlalchemy and pylons. It basically takes a haiku, writes it to a database, and displays the haiku. The problem appears when I get the data from the form and try and write it to a database, Pylons give me this error: AttributeError: 'tuple' object has no attribute 'insert' after I run this line of code: ins = self.haiku_table.insert(values=form_dict)
Main Code:
import logging
from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to
from myku.lib.base import BaseController, render
from sqlalchemy.sql import select
import meta
import myku.lib.helpers as h
log = logging.getLogger(__name__)
class IndexController(BaseController):
def __init__(self):
self.haiku_table = meta.haiku_table
self.conn = meta.engine.connect()
BaseController.__init__(self)
def index(self, genre, title):
ss = selec开发者_Go百科t([self.haiku_table], self.haiku_table.c.genre==str(genre).lower(), self.haiku_table.c.title==str(title).lower())
result = self.conn.execute(ss)
return result
def new_haiku(self):
return render('/newku.html')
def submit(self):
title = request.params.get('title')
haiku = request.params.get('haiku')
genre = request.params.get('genre')
author = request.params.get('author')
form_dict = {'title': title, 'haiku': haiku, 'genre': genre, 'author': author}
ins = self.haiku_table.insert(values=form_dict)
result = self.conn.execute(ins)
return res
and the code for the meta file:
from sqlalchemy.engine import create_engine
from sqlalchemy import schema, types
metadata = schema.MetaData()
haiku_table = ('haiku', metadata,
schema.Column('title', types.Text(), primary_key=True),
schema.Column('haiku', types.Text()),
schema.Column('genre', types.Text()),
schema.Column('author', types.Text())
)
engine = create_engine('sqlite:///F:\\MyKu\\myku\\haiku')
metadata.bind = engine
metadata.create_all(checkfirst=True)
Any ideas? I have no clue
Well, it looks like you're creating haiku_table
and not doing anything else to it before trying to use the .insert function which obviously is not part of a tuple
Looks like when you create a table with SQLAlchemy, you want the format:
haiku_table = Table('haiku', metadata,
schema.Column('title', types.Text(), primary_key=True),
.... etc
)
You will need to import Table
from the sqlachlemy module as well.
This makes haiku_table be a Table instance of SQLAlchemy and not simply a tuple. I think that's all you're missing.
精彩评论