On Fri, Jul 4, 2014 at 11:19 AM, 'Frank Liou' via sqlalchemy
<[email protected]> wrote:
> I try to insert username in to my table
>
> it show
>
> Internal Server Error
> The server encountered an internal error and was unable to complete your
> request. Either the server is overloaded or there is an error in the
> application.
>
> it maybe mean no request
>
> i try to change username to '123123'
>
> then it works....
>
> what's problem with this?
>
>
>
> @app.route('/user/<username>',methods=['GET','POST'])
> def hello(username):
> if request.method=='POST':
> save_friends(username)
> return username
>
>
> def save_friends(username):
> conn = engine.connect()
> conn.execute("INSERT INTO friends(name) VALUES(username)")
>
Python doesn't automatically replace variable names in strings with
their values, so you are sending this exact string to the database:
INSERT INTO friends(name) VALUES(username)
...which is not valid SQL.
Instead, you should use SQLAlchemy's text() function and bound
parameters to pass the actual username to the database, something like
this:
import sqlalchemy as sa
def save_friends(username):
conn = engine.connect()
statement = sa.text('INSERT INTO friends(name) VALUES(:username)')
conn.execute(statement, username=username)
Hope that helps,
Simon
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.