how can i get .engine.execute result?

566 views
Skip to first unread message

Frank Liou

unread,
Jul 3, 2014, 3:48:22 AM7/3/14
to sqlal...@googlegroups.com
def get_query():
    conn = engine.connect()
    check = 'SELECT * FROM friends'
    obj = [conn.execute(check)]
    jqs = json.dumps(obj)
    return jqs



result is 

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.


if i change obj = '123'

it will show 123


but when i use execute to query

it will no request

how can that be?

is it wrong type to catch?


pleaese help me .thanks

Simon King

unread,
Jul 3, 2014, 6:44:44 AM7/3/14
to sqlal...@googlegroups.com
Firstly, when you get a message like that from your web server, you
can normally find the full error message in the web server logs. The
location of those depends on how the server has been set up, but they
are usually somewhere like /var/log/httpd/error_log or
/var/log/apache2/error_log on Linux.

If you looked in there, I think you would probably see a stack trace
ending with a message about being unable to convert a ResultProxy to
json. After this:

obj = [conn.execute(check)]

"obj" is now a list containing a single element, which is a
ResultProxy object. You probably wanted something like this instead:

obj = conn.execute(check).fetchall()

After running that, obj will now be a list of rows from the database.
However, you still won't be able to convert it directly to json,
because each row is a RowProxy object, and the json library doesn't
know how to handle RowProxy objects.

The simplest thing would probably be to convert each row to a dictionary:

rows = conn.execute(check).fetchall()
obj = [dict(row.items()) for row in rows]

Hope that helps,

Simon

Frank Liou

unread,
Jul 4, 2014, 1:54:07 AM7/4/14
to sqlal...@googlegroups.com

Hi Simon

thanks for your answer

but i can't understand what's "fetchall()" mean

i remove the  " fetchall()"  it still work....

 

Simon King

unread,
Jul 4, 2014, 5:08:34 AM7/4/14
to sqlal...@googlegroups.com
This is because the ResultProxy returned from conn.execute() is what
is known in Python as "iterable", which basically means that you can
iterate over it (for example, use it in a "for" loop or a list
comprehension).

The main catch is that you can only iterate over the ResultProxy once,
so the following wouldn't work:

rows = conn.execute(check)

# This line iterates over the ResultProxy, consuming it in the process
obj1 = [dict(row.items()) for row in rows]

# This line will not do what you expect because
# the ResultProxy has been consumed
obj2 = [dict(row.items()) for row in rows]

If you use .fetchall(), then "rows" is a simple python list, so you
can iterate over it as many times as you like. For what you are trying
to do, you don't need to iterate over it more than once, so it doesn't
really matter which you do.

There are other things to be aware of. You can't necessarily use the
len() function to get the length of an arbitrary iterable, you can't
sort them in place, and so on.

Hope that helps,

Simon
Reply all
Reply to author
Forward
0 new messages