row = cursor.fetchone() / row = cursor.fetchall()

480 views
Skip to first unread message

Nebros

unread,
Nov 22, 2012, 9:27:34 AM11/22/12
to django...@googlegroups.com
Hello, i know I have asked this before in another post of me, but no one answered on it and it was not part of topic...
 
I am using pyodbc and try to give data out into my page...
 
Here the part of pyodbc (and yes this works):
 
views---------------------------------------------------------
.....
1    conn = pyodbc.connect('DRIVER={SQL Server};SERVER=MAURITIUS;DATABASE=baan5c;UID=***;PWD=*****')
2   cursor = conn.cursor()
3    cursor.execute("SELECT x.t_name, y.t_mail FROM tttaad200000 as x, tttcmf200000 as y WHERE (x.t_name = y.t_name)")
4    row = cursor.fetchall()
5
6    return render_to_response("kundendaten.html", { 'row': row}, context_instance=RequestContext(request))
.....
-----------------------------------------------------------------
 
On line number 4 you can see now, there is "row = cursor.fetchall()"... When i give out this part now i become two outputs, (one without formatting to see what value i become, the other have to be in a table)
 
Here the parts of my website:
 
Kundendaten-------------------------------------
.....
{% block content2 %}Kundendaten{{ row }}.{% endblock %}
.....
    <tr>
        <th>Name</th>
        <th>Mail</th>
    </tr>
    {% for t_user in row %}
        <tr>
            <td>{{ row.t_name }}</td>
            <td>{{ row.t_mail }}</td>
        </tr>
    {% endfor %}
.....
------------------------------------------------------
 
1.:
[('Ackermann Marcel (THA) ', 'marcel.ackermann@***.ch '), ('Adami Renate (HEI) ', 'renate.adami@*.ch '), ('Ammann Cornelia (HEI) ', 'cornelia.ammann@*.ch '), ('Ausbildung A. Schwaller (HEI) ', 'giulia.franco@*.ch '), ('General_User_NT (SYS) ', 'baan-alarm@*.ch '), ('Benz Roger (THA) ', 'roger.benz@*.ch '), .....
 
2.:
(A table with only name and mail as colum titles and but 2 empty lines)
 
When i add now on line 3 by views t_user like this:
3 cursor.execute("SELECT x.t_name, x.t_user,  y.t_mail FROM tttaad200000 as x, tttcmf200000 as y WHERE (x.t_name = y.t_name)")
I become a longer output... and a table with 3 empty lines
 
When i use it now with fetchone and two selects:
4 row = cursor.fetchone()
I become a table like this:
 
NameMail
Ackermann Marcel (THA) marcel.ackermann@***.ch
Ackermann Marcel (THA)

marcel.ackermann@***.ch

And with tree selects there will be tree lines with same value...
 
Now my question:
How can i make my output correct with fetchall that i can see all lines with the correct values?

Nebros

unread,
Nov 22, 2012, 9:42:46 AM11/22/12
to django...@googlegroups.com
EDIT: with fetchall the output is a table with a lot of empty lines. just with fetchone the lines are diffrent in count.
fetchone: so many filled lines like variables ( name, mail, user,...)
fetchall: so many empty lines like values like diffrent users there are...

Tom Evans

unread,
Nov 22, 2012, 10:42:48 AM11/22/12
to django...@googlegroups.com
On Thu, Nov 22, 2012 at 2:27 PM, Nebros <markusch...@gmail.com> wrote:
Hello, i know I have asked this before in another post of me, but no one answered on it and it was not part of topic...

In fact, I did, but you ignored all my advice, didn't fix the problems I pointed out, and still present with the same issue.
Again. If you say "for t_user in row", then you iterate over the list "row", giving a variable inside the loop named "t_user" for each element in the list. You then don't actually use "t_user" inside the loop, you use "row". "row" is your list of rows, not an element in the list. 

This is why you get 3 empty lines in your table - there are three items in "row", but there are no attribute on "row" named "t_name" or "t_mail" - they are on the items inside "row". 

In fact, each item in row does not have those attributes. Lets look at what fetchall() returns:

>>> c.execute("SELECT id, name from auth_group WHERE id < 4")
3L
>>> c.fetchall()
((2L, u'Client Super User'), (3L, u'Helpdesk User'), (1L, u'IT User'))

So, fetchall() returns a tuple of tuples. If you iterate through this in your template, then you must refer to each variable by its position - there is no "t_name" to refer to. Eg:

{% for row in rows %}
  <tr>
    <td>{{ row.0 }}</td>
    <td>{{ row.1 }}</td>
  </tr>
{% endfor %}

Tom

Nebros

unread,
Nov 23, 2012, 2:31:30 AM11/23/12
to django...@googlegroups.com, teva...@googlemail.com
I saw your answer and it was long and with a lot of time, i thanks for that. I have removed {% block content %} and i did other changes, like you sayed.
 
>>> c.execute("SELECT id, name from auth_group WHERE id < 4")
3L
>>> c.fetchall()
((2L, u'Client Super User'), (3L, u'Helpdesk User'), (1L, u'IT User'))
 
^
|
| This part i didnt understand what you mean, but doesent matter. :)
 
And:
{% for row in rows %}
<tr>
<td>{{ row.0 }}</td>
<td>{{ row.1 }}</td>
</tr>
{% endfor %}
 
^
|
| This part was, what i have searched for. I am very new in Python/Django and what I can do is yust copy/paste and understand what I have copied, but I cant really create my own code atm.
 
It works now, and I am really happy about that. :)
Now there is only one problem left, how can i use my value out of my form and use it as a variable? ^^
 
Thank you very much Tom, you was very helpful. =)

Nebros

unread,
Nov 26, 2012, 10:09:38 AM11/26/12
to django...@googlegroups.com, teva...@googlemail.com
I found the answer by myself...
it was just to add this:
 
    ret = request.POST
    form = ret['kunde']
 
and use this "form" as a variable... "%s" %form
Reply all
Reply to author
Forward
0 new messages